🏷️ nginx
🏷️ https
🏷️ docker compose
수정일 : 2024-11-15
배경
- 테커 부트캠프를 진행중이다.
- 모든 프로그램은 docker-compose로 구성되어 있다.
- AWS EC2에 구동 중인 서버에 HTTPS를 적용하려고 한다.
- 도메인 구매 없이 시도를 했으나, AWS에서 제공하는 도메인으로 SSL 인증서를 발급받을 수 없었다.
- 따라서, 도메인을 구매하고, Route 53을 통해 도메인을 연결했다.
목표
방법
1. docker-compose.yml에 certbot 컨테이너를 추가한다.
1certbot:
2 image: certbot/certbot
3 container_name: certbot
4 volumes:
5 - ./certbot/conf:/etc/letsencrypt
6 - ./certbot/www:/var/www/certbot
7 depends_on:
8 - nginx
9
10 # certbot을 무한루프로 돌리기 위해 사용
11 entrypoint: "/bin/sh -c 'trap exit TERM; while :; do sleep 6h & wait $${!}; done;'"
2. nginx.conf를 수정한다.
# certbot을 사용하기 위한 설정
location /.well-known/acme-challenge/ {
allow all;
root /var/www/certbot;
}
3. certbot 컨테이너를 활용해서 SSL 인증서를 발급받는다.
1docker exec -it certbot certbot certonly \
2 # 웹 루트 방식으로 인증서를 생성
3 --webroot \
4 # 웹 서버의 웹 루트 디렉터리 경로를 지정
5 --webroot-path=/var/www/certbot \
6 # 인증서 갱신 및 중요한 알림을 받을 이메일 주소를 지정
7 --email {이메일 주소} \
8 # Let's Encrypt 서비스 약관에 동의
9 --agree-tos \
10 # EFF(Electronic Frontier Foundation) 뉴스레터를 받지 않도록 설정
11 --no-eff-email \
12 # SSL 인증서를 생성할 도메인 이름을 지정
13 -d {도메인 이름}
4. Nginx 웹 서버와 함께 사용할 SSL 설정 파일을 다운로드
- 다운 받은 후 파일을 알맞은 위치로 이동시킨다.
- 해당 프로젝트에서는 /etc/letsencrypt/로 이동시켰다.
1sudo curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "./options-ssl-nginx.conf"
2
3sudo curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "./ssl-dhparams.pem"
5. nginx.conf를 수정한다.
server {
listen 80;
charset utf-8;
server_name {도메인 이름};
# HTTP 요청을 HTTPS로 리다이렉트
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
charset utf-8;
server_name { 도메인 이름 };
# SSL 인증서 설정
ssl_certificate /etc/letsencrypt/live/api.forest-of-thoughts.site/fullchain.pem;
# SSL 인증서 키 설정
ssl_certificate_key /etc/letsencrypt/live/api.forest-of-thoughts.site/privkey.pem;
# SSL 설정 파일 포함
include /etc/letsencrypt/options-ssl-nginx.conf;
# Diffie-Hellman 키 설정
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
6. nginx 컨테이너 설정을 수정한다.
1nginx:
2 image: nginx:stable
3 ports:
4 - "80:80"
5 - "443:443"
6 volumes:
7 - ./nginx.conf:/etc/nginx/nginx.conf
8 - ./certbot/conf:/etc/letsencrypt
9 - ./certbot/www:/var/www/certbot
회고
- 보통 crontab을 활용해서 자동으로 인증서 갱신을 받는다.
- 이번에는 프로젝트 기간이 길지 않아서, 수동으로 진행했다.
- 다음에는 자동으로 인증서 갱신을 받는 것도 도전해보자.