๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Server

[Nginx, Certbot] ์„œ๋ธŒ๋„๋ฉ”์ธ์— SSL ์ธ์ฆ์„œ ๋ฐœ๊ธ‰ ํ›„ https ์„ค์ •ํ•˜๊ธฐ

by LasBe 2024. 11. 19.
๋ฐ˜์‘ํ˜•

๐Ÿ“’ [Nginx, Certbot] ์„œ๋ธŒ๋„๋ฉ”์ธ์— SSL ์ธ์ฆ์„œ ๋ฐœ๊ธ‰ ํ›„ https ์„ค์ •ํ•˜๊ธฐ


์ €๋ฒˆ๋‹ฌ์— ํฌํŠธํด๋ฆฌ์˜ค ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ๋„๋ฉ”์ธ์„ ํ•˜๋‚˜ ๊ตฌ๋งคํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ์— ์ƒˆ๋กœ์šด ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉฐ Nexus Docker Repository๋ฅผ ์„ค์น˜ํ–ˆ๋Š”๋ฐ, ์„œ๋ธŒ ๋„๋ฉ”์ธ์— SSL ์ธ์ฆ์„œ๋ฅผ ์ ์šฉํ•ด https๋กœ ํ†ต์‹ ํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒŒ ์ž์ฃผ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž‘์—…์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธˆ๋ฐฉ ๊ธฐ์–ต์—์„œ ์ง€์›Œ์งˆ ๊ฒƒ ๊ฐ™์•„ ๊ธฐ๋กํ•ด ๋ด…๋‹ˆ๋‹ค.

 

๐Ÿ“Œ CNAME  ๋ ˆ์ฝ”๋“œ ์„ค์ •

CNAME์€ ํ•œ ๋„๋ฉ”์ธ ์ด๋ฆ„์„ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ ์ด๋ฆ„์— ๋ณ„์นญ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

์ €๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด "lasbe.kr"์ด๋ž€ ๋„๋ฉ”์ธ์„ ๊ตฌ๋งคํ–ˆ๊ณ , docker repository ํ†ต์‹ ์„ ์œ„ํ•ด CNAME ๋ ˆ์ฝ”๋“œ๋ฅผ "docker"๋กœ ์„ค์ •ํ–ˆ์œผ๋ฉด ์ตœ์ข…์ ์œผ๋กœ "docker.lasbe.kr"๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ํ˜ธ์ŠคํŒ…์ผ€์ด์•Œ์ด๋ผ๋Š” ๋„๋ฉ”์ธ ์—…์ฒด์—์„œ CNAME์„ ์œ„์™€ ๊ฐ™์ด ์„ค์ •ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ”Ž CNAME  ๋ ˆ์ฝ”๋“œ ํ™•์ธ

https://ultahost.com/ko/domain-dns-lookup

 

DNS ์กฐํšŒ ๋„๊ตฌ - ์ฆ‰์‹œ DNS ๋ ˆ์ฝ”๋“œ ํ™•์ธ | ์–ผํƒ€ํ˜ธ์ŠคํŠธ

DNS ์กฐํšŒ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DNS ๋ ˆ์ฝ”๋“œ๋ฅผ ์ฆ‰์‹œ ํ™•์ธํ•˜์„ธ์š”. ์›น์‚ฌ์ดํŠธ ๋ฐ ๋„คํŠธ์›Œํฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ํšจ์œจ์ ์ธ DNS ๊ฒ€์‚ฌ๊ธฐ์ž…๋‹ˆ๋‹ค.

ultahost.com

$ nslookup ๋ช…๋ น์–ด๋‚˜ ์œ„ ์›นํŽ˜์ด์ง€๋ฅผ ํ†ตํ•ด CNAME์ด ์ž˜ ์ ์šฉ๋˜์—ˆ๋‚˜ ํ™•์ธํ•ด ์ค๋‹ˆ๋‹ค.

certbot์„ 5๋ฒˆ ์‹คํ–‰ํ•˜๋ฉด 1์‹œ๊ฐ„์„ ๊ธฐ๋‹ค๋ ค์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ์ž˜ ์ ์šฉ๋˜์—ˆ๋‚˜ ํ™•์ธ ๋จผ์ € ํ•ด์ฃผ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“Œ SSL ์ธ์ฆ์„œ ๋ฐœ๊ธ‰

๊ธฐ์กด ๋„๋ฉ”์ธ์— ์ธ์ฆ์„œ๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์•˜๋‹ค๋Š” ์ „์ œ ํ•˜์— ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ๊ธฐ์กด ๋„๋ฉ”์ธ์— ์ธ์ฆ์„œ๋ฅผ ๋ฐœ๊ธ‰ ๋ฐ›์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

Proxy Server์— Certbot์œผ๋กœ SSL ์ธ์ฆ์„œ ๋ฐœ๊ธ‰ ๋ฐ›๊ณ  Https ์ ์šฉํ•˜๊ธฐ

 

[Docker + Nginx] Proxy Server์— Certbot์œผ๋กœ SSL ์ธ์ฆ์„œ ๋ฐœ๊ธ‰ ๋ฐ›๊ณ  Https ์ ์šฉํ•˜๊ธฐ

โšก๏ธ Certbot์œผ๋กœ SSL ์ธ์ฆ์„œ ๋ฐœ๊ธ‰๋ฐ›๊ณ  Https ์ ์šฉํ•˜๊ธฐ ๊ฐœ์ธ ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ 8080 ํฌํŠธ๋กœ ์˜ฌ๋ฆฌ๊ณ , 80 ํฌํŠธ์—๋Š” Nginx๋ฅผ ์ด์šฉํ•ด ํ”„๋ก์‹œ ์„œ๋ฒ„๋ฅผ ์˜ฌ๋ ค ์–ธ์ œ๋“  80 ํฌํŠธ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋‹ค์Œ

lasbe.tistory.com

 

๐Ÿ”Ž ๊ธฐ์กด ์ธ์ฆ์„œ ํ™•์ธ

$ certbot certificates

์œ„ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ๊ธฐ์กด์— ๋ฐœ๊ธ‰๋ฐ›์•˜๋˜ ์ธ์ฆ์„œ๋ฅผ ํ™•์ธํ•ด ์ค๋‹ˆ๋‹ค.

 

๐Ÿ”Ž ์ธ์ฆ์„œ ์—…๋ฐ์ดํŠธ

$ certbot certonly --cert-name <์ธ์ฆ์„œ ์ด๋ฆ„> -d <๋„๋ฉ”์ธ1> -d <๋„๋ฉ”์ธ2> ...

$ certbot certonly --cert-name lasbe.kr -d docker.lasbe.kr -d lasbe.kr

 

๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ์„ ํƒ์‚ฌํ•ญ์ด ๋‚˜์˜ค๋Š”๋ฐ, ์ € ๊ฐ™์€ ๊ฒฝ์šฐ nginx๋ฅผ ํ†ตํ•ด ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 1๋ฒˆ์„ ์„ ํƒํ–ˆ๊ณ  ๋ฐœ๊ธ‰์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ Nginx ์„ค์ •ํŒŒ์ผ ์ถ”๊ฐ€

/etc/nginx/conf.d/default.conf ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์—ฌ ์ธ์ฆ์„œ๋ฅผ ์ ์šฉํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ”Ž ์„œ๋ฒ„ ๋ธ”๋ก ์ถ”๊ฐ€

server {
    server_name  docker.lasbe.kr;

    location / {
        proxy_pass http://192.168.0.8:5000;
    }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/lasbe.kr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/lasbe.kr/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

๊ธฐ์กด ์„ค์ •ํŒŒ์ผ์— ์œ„์™€ ๊ฐ™์ด ์„œ๋ฒ„ ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์„œ๋ธŒ ๋„๋ฉ”์ธ์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ๋ผ์šฐํŒ… ํ•ด์ค๋‹ˆ๋‹ค.

443 ํฌํŠธ listen๊ณผ ์ธ์ฆ์„œ ๋ถ€๋ถ„์€ ๊ธฐ์กด์— ์กด์žฌํ•˜๋˜ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌ-๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ”Ž http -> https

server {
    listen 80;
    server_name *.lasbe.kr lasbe.kr;
    return 301 https://$host$request_uri;
}

http๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ https๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ ๋„๋ฉ”์ธ๋งˆ๋‹ค ์ฒ˜๋ฆฌํ•˜๊ธด ํž˜๋“œ๋‹ˆ, ์œ„ ์„œ๋ฒ„ ๋ธ”๋ก์„ ํ†ตํ•ด http๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ์ผ๊ด„์ ์œผ๋กœ https ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์ฒ˜๋ฆฌํ•ด ์ค๋‹ˆ๋‹ค.

 

๐Ÿ”Ž ์ „์ฒด ์„ค์ •ํŒŒ์ผ

server {
    listen 80;
    server_name *.lasbe.kr lasbe.kr;
    return 301 https://$host$request_uri;
}

server {
    server_name  lasbe.kr;

    location / {
        proxy_pass http://192.168.0.8:3000;
    }

    location /.well-known/acme-challenge/ {
        allow all;
        root /var/www/certbot;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/lasbe.kr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/lasbe.kr/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    server_name  docker.lasbe.kr;

    location / {
        proxy_pass http://192.168.0.8:5000;
    }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/lasbe.kr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/lasbe.kr/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

์‹ค์ œ ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋Š” Nginx ์„œ๋ฒ„ ์„ค์ •ํŒŒ์ผ์˜ ์ผ๋ถ€๋Š” ์œ„์™€ ๊ฐ™์œผ๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค.

 

๐Ÿ”Ž ์„ค์ •ํŒŒ์ผ ๋ฐ˜์˜

$ sudo nginx -t
$ sudo service nginx reload

Nginx ํ…Œ์ŠคํŠธ ํ›„ ์ •์ƒ์ด๋ผ๋ฉด ๋ฐ˜์˜ํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


์˜คํ”ˆ ์ฑ„ํŒ