From a124139e16b17dce08425a6472272b1b9f4e5758 Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Tue, 15 Oct 2024 23:50:38 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20nginx,=20https=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 15 +++++ init-letsencrypt.sh | 80 +++++++++++++++++++++++++++ nginx/conf.d/nginx.conf | 32 ++++++++--- src/main/resources/application-db.yml | 4 +- 4 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 init-letsencrypt.sh diff --git a/docker-compose.yml b/docker-compose.yml index 61c4219..1e883ba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,11 +20,26 @@ services: ports: - 80:80 - 443:443 + volumes: + - ./nginx/config:/etc/nginx/conf.d + - ./nginx/certbot/conf:/etc/letsencrypt + - ./nginx/certbot/www:/var/www/certbot + command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'" depends_on: - application networks: - chekirout-network # 네트워크 연결 추가 + certbot: + image: certbot/certbot:latest + restart: unless-stopped + volumes: + - ./nginx/certbot/conf:/etc/letsencrypt + - ./nginx/certbot/www:/var/www/certbot + networks: + - chekirout-network + entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'" + networks: chekirout-network: driver: bridge \ No newline at end of file diff --git a/init-letsencrypt.sh b/init-letsencrypt.sh new file mode 100644 index 0000000..9f52ad3 --- /dev/null +++ b/init-letsencrypt.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +if ! [ -x "$(command -v docker-compose)" ]; then + echo 'Error: docker-compose is not installed.' >&2 + exit 1 +fi + +domains=(dev.chekirout.com) +rsa_key_size=4096 +data_path="./nginx/certbot" +email="kmina591208@gmail.com" +staging=0 + +if [ -d "$data_path" ]; then + read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision + if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then + exit + fi +fi + + +if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then + echo "### Downloading recommended TLS parameters ..." + mkdir -p "$data_path/conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf" + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem" + echo +fi + +echo "### Creating dummy certificate for $domains ..." +path="/etc/letsencrypt/live/$domains" +mkdir -p "$data_path/conf/live/$domains" +docker-compose run --rm --entrypoint "\ + openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\ + -keyout '$path/privkey.pem' \ + -out '$path/fullchain.pem' \ + -subj '/CN=localhost'" certbot +echo + + +echo "### Starting nginx ..." +docker-compose up --force-recreate -d nginx +echo + +echo "### Deleting dummy certificate for $domains ..." +docker-compose run --rm --entrypoint "\ + rm -Rf /etc/letsencrypt/live/$domains && \ + rm -Rf /etc/letsencrypt/archive/$domains && \ + rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot +echo + + +echo "### Requesting Let's Encrypt certificate for $domains ..." +#Join $domains to -d args +domain_args="" +for domain in "${domains[@]}"; do + domain_args="$domain_args -d $domain" +done + +# Select appropriate email arg +case "$email" in + "") email_arg="--register-unsafely-without-email" ;; + *) email_arg="--email $email" ;; +esac + +# Enable staging mode if needed +if [ $staging != "0" ]; then staging_arg="--staging"; fi + +docker-compose run --rm --entrypoint "\ + certbot certonly --webroot -w /var/www/certbot \ + $staging_arg \ + $email_arg \ + $domain_args \ + --rsa-key-size $rsa_key_size \ + --agree-tos \ + --force-renewal" certbot +echo + +echo "### Reloading nginx ..." +docker-compose exec nginx nginx -s reload \ No newline at end of file diff --git a/nginx/conf.d/nginx.conf b/nginx/conf.d/nginx.conf index 316dd5f..cad7ea5 100644 --- a/nginx/conf.d/nginx.conf +++ b/nginx/conf.d/nginx.conf @@ -1,24 +1,38 @@ server { listen 80; + listen [::]:80; server_name *.amazonaws.com dev.chekirout.com; - access_log /var/log/nginx/access.log; + server_tokens off; - if ($http_x_forwarded_proto = 'http') { - return 301 https://$host$request_uri; + location /.well-known/acme-challenge/ { + allow all; + root /var/www/certbot; } location / { - proxy_pass http://chekirout-springboot-dev:8080; - proxy_set_header Host $host:$server_port; - proxy_set_header X-Forwarded-Host $server_name; + return 301 https://$host$request_uri; + } +} + +server { + listen 443 ssl; + server_name dev.chekirout.com; + server_tokens off; + + ssl_certificate /etc/letsencrypt/live/dev.chekirout.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/dev.chekirout.com/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + client_max_body_size 20m; + + location / { + proxy_pass http://172.17.0.1:8080; + proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Authorization $http_authorization; proxy_pass_header Authorization; - # CORS 관련 헤더 처리를 제거 - # Nginx는 CORS 헤더를 추가하지 않고 Spring Boot가 처리하도록 맡김 - if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' "$http_origin" always; add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; diff --git a/src/main/resources/application-db.yml b/src/main/resources/application-db.yml index ebc5635..1c5d6ac 100644 --- a/src/main/resources/application-db.yml +++ b/src/main/resources/application-db.yml @@ -38,7 +38,7 @@ spring: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/chekirout username: root - password: root1234 + password: 1234 p6spy: enabled: true appender: com.p6spy.engine.spy.appender.Slf4JLogger @@ -46,7 +46,7 @@ spring: p6spy: "%(currentTime)|%(executionTime)|%(category)|%(sqlSingleLine)" jpa: hibernate: - ddl-auto: update + ddl-auto: create properties: hibernate: dialect: org.hibernate.dialect.MySQLDialect