HTTPS 사용하기
로컬에서는 HTTP 요청으로도 테스트 할만하지만 배포 환경에서 HTTP 만으로는 제한되는 부분이 조금 있다.
특히 쿠키를 사용할 때 크롬 정책으로 인해 쿠키 확인이 너무나도 힘들다.
프론트와 백엔드가 같은 도메인인 경우에는 Strict 쿠키 정책으로 어떻게 HTTP로도 가능하겠지만,
다른 도메인이라면 방법이 없다.
그래서 인증서를 발급받고 SSL을 적용해서 HTTPS 요청을 사용하여 Secure 쿠키 정책을 사용해야 한다.
HTTPS는 인증서, 암호화 이런 단어가 들어있어서 어려울것 같지만 생각보다 간단하고 빠르게 할 수 있다.
사용 기술
- EC2
- Nginx
- certbot
1. SSL 적용
SSL을 적용하기 위해서는 인증서가 필요하다.
전에는 Route53에서 매월 0.5달러인가 주고 했지만 이번에는 certbot으로 무료로 해보려고 한다.
certbot의 공식 문서 를 참고하면 아래와 같은 과정을 거친다.
사실상 그대로 가져왔으니 공식 문서만 봐도 충분히 따라할 수 있다.
# 1. certbot을 snap 명령어로 설치 및 실행하므로 snap을 먼저 설치한다
sudo snap install core
sudo snap refresh core
# 2. 기존에 설치된 certbot을 제거한다
# 공식 가이드에선 certbot명령어를 사용할 때 snap이 사용되게 하기 위함이라고 되어있다.
sudo apt-get remove certbot
# 3. certbot을 설치한다
sudo snap install --classic certbot
# 4. certbot 명령어가 실행될 수 있게 세팅한다
sudo ln -s /snap/bin/certbot /usr/bin/certbot
# 5. 아래 명령어를 입력하면 certbot이 nginx 구성을 자동으로 수정한다.
# nginx가 아닌 apache를 웹서버로 사용할 경우, sudo certbot --apache 가 된다
sudo certbot --nginx -d midcon.store -d www.midcon.store
# certbot은 CLI몇 줄로 SSL을 적용해줄 뿐 아니라 자동 리뉴얼까지 해준다
# 처음 설치할 때부터 이러한 cron job 처리를 위한 내용을 site-available/default 에 자동으로 설정해준다
# 아래 명령어로 자동 리뉴얼이 적용되고 있는지 확인할 수 있다
sudo certbot renew --dry-run
5번 커맨드를 입력하면 아래처럼 이메일, 약관 동의 여부 등등을 입력하는 부분이 있다.
당황하지말고 적당히 읽어보고 입력하면 된다.
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): hyukkind@naver.com # 이메일 입력
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y # ACME 약관에 동의하는지 N선택시 진행불가
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y # 이메일을 통해 Let's Encrypt 프로젝트 정보를 받아볼지
Account registered.
Requesting a certificate for midcon.store and www.midcon.store
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/midcon.store/fullchain.pem
Key is saved at: /etc/letsencrypt/live/midcon.store/privkey.pem
This certificate expires on 2024-08-15.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for midcon.store to /etc/nginx/sites-enabled/default
Successfully deployed certificate for www.midcon.store to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://midcon.store and https://www.midcon.store
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2. 결과 확인
2-1. /etc/nginx/sites-available/default 확인
아래 사진은 /etc/nginx/sites-available/default 로 들어가본 결과이다.
localtion /ws 부분은 본인이 웹소켓 설정하느라 추가한 부분이고 아래 부분부터 보면 되는데
아래처럼 certbot이 nginx 설정까지 자동으로 변경한 알 수 있다.
HTTP로 요청하면 HTTPS로 리다이렉트 시키는 설정도 certbot이 자동으로 해두었다.
2-2. HTTP/HTTPS 요청 시
이제 HTTP 로 요청하면 자동으로 HTTPS 로 리다이렉트 되고, HTTPS 요청도 잘 되는걸 확인할 수 있다.
3. 추가
만약 인증서를 삭제하고 싶다면 아래 명령어를 입력하면 된다.
sudo certbot delete
그럼 아래와 같은 창을 확인할 수 있고, 여기서 삭제하고 싶은 인증서의 번호를 입력하면 된다.
아쉽지만 nginx 설정까지는 자동으로 삭제해주지는 않는것 같다.
인증서 삭제 후 nginx 변경 부분은 수동으로 삭제하자.
참고자료
10분만에 끝내는 EC2 생성, NGINX 구성, SSL적용
이 포스팅에선 이론적인 내용에 보다는 구성 방법과 흐름에 대해서만 조망합니다. EC2 생성, NGINX 설치, 프록시 설정, 도메인 및 SSL 적용을 해본 적이 없거나 과정에 대해 모호한 부분이 있으시다
creampuffy.tistory.com
Certbot으로 무료 HTTPS 인증서 발급받기
Let’s Encrypt - Free SSL/TLS CertificatesLet’s Encrypt is a free, automated, and open certificate authority brought to you by the nonprofit Internet Security Research Group (ISRG).Free SSL/TLS Certificates Let's Encrypt라는 비영리 기관을 통해
www.vompressor.com
'DevOps > 서비스 배포' 카테고리의 다른 글
도메인 연결 1: Nginx로 도메인으로의 요청 포워딩 (0) | 2024.04.29 |
---|---|
EC2 인스턴스 타임존 설정 (0) | 2024.03.29 |
EC2 프리티어 성능 보완을 위한 Swap Memory 설정 (0) | 2024.03.27 |
AWS 배포 6: 보안 그룹 설정 (0) | 2024.02.29 |
AWS 배포 5: EC2에서 Spring Boot 서버 띄우기 (0) | 2024.02.28 |