http에서는 쿠키가 적용되지 않아 SSL을 적용하려 한다. docker-compose, nginx, springboot 환경에서 무료로 SSL 인증서를 적용하고 shell script를 통해 인증서 유효기간이 만료되기 전에 자동으로 기간을 연장해보자.

사전 준비 사항

인증서 발급을 위한 certbot 컨테이너 만들기

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

nginx 설정파일 변경

Before

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;
        }
    }
}

After