http에서는 쿠키가 적용되지 않아 SSL을 적용하려 한다. docker-compose, nginx, springboot 환경에서 무료로 SSL 인증서를 적용하고 shell script를 통해 인증서 유효기간이 만료되기 전에 자동으로 기간을 연장해보자.
version: '3'
services:
backend:
container_name: backend
image: {springboot 이미지 이름}
ports:
- "8080:8080"
user: "1000:1000"
networks:
- anifriends
volumes:
- /home/ec2-user/logs:/logs
nginx:
image: {nginx 이미지 이름}
container_name: nginx
volumes:
- ./nginx/:/etc/nginx/
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
ports:
- "80:80"
- "443:443"
depends_on:
- backend
networks:
- anifriends
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \\"daemon off;\\"'"
certbot:
container_name: certbot
image: certbot/certbot
restart: unless-stopped
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
networks:
- anifriends
redis:
container_name: redis
image: redis:latest
depends_on:
- backend
ports:
- "6379:6379"
networks:
- anifriends
networks:
anifriends:
volumes:
logs:
certbot: # 추가
container_name: certbot
image: certbot/certbot
restart: unless-stopped
volumes:
- ./data/certbot/conf:/etc/letsencrypt # 추가
- ./data/certbot/www:/var/www/certbot # 추가
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
networks:
- anifriends
certbot 컨테이너
추가nginx
에 certbot
에 대한 volume 2개
추가
certbot
이 발급한 인증서를 nginx
가 브라우져의 요청에 따라 반환할 수 있어야 하므로 certbot
컨테이너와 nginx
컨테이너는 같은 폴더를 공유해야 한다.nginx
에 443 포트 열기nginx의 command
, certbot의 entrypoint
는 인증서가 만료되기 전에 자동으로 SSL 인증서를 재발급해준다.events {}
http {
server {
listen 80;
server_name localhost;
location / {
proxy_pass <http://backend:8080>;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}