Skip to content

Commit

Permalink
chore: nginx, https ์„ค์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogu1208 committed Oct 15, 2024
1 parent fc9045d commit a124139
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 11 deletions.
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
80 changes: 80 additions & 0 deletions init-letsencrypt.sh
Original file line number Diff line number Diff line change
@@ -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="[email protected]"
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
32 changes: 23 additions & 9 deletions nginx/conf.d/nginx.conf
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ 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
logMessageFormat:
p6spy: "%(currentTime)|%(executionTime)|%(category)|%(sqlSingleLine)"
jpa:
hibernate:
ddl-auto: update
ddl-auto: create
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
Expand Down

0 comments on commit a124139

Please sign in to comment.