๐ŸNginx๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ https ์š”์ฒญ ์ฒ˜๋ฆฌํ•˜๊ธฐ

๊น€์ข…์™„ยท2022๋…„ 8์›” 12์ผ
1

Nginx๋ž€?

Nginx๋ž€ ์•„ํŒŒ์น˜, IIS์™€ ๊ฐ™์€ ์›น ์„œ๋ฒ„ ์†Œํ”„ํŠธ์›จ์–ด์ž…๋‹ˆ๋‹ค. ๊ฐ€๋ณ๊ณ  ๋†’์€ ์„ฑ๋Šฅ์„ ๋ชฉํ‘œ๋กœ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ์ตœ๊ทผ ๋“ค์–ด์„œ ์•„ํŒŒ์น˜ ๋‹ค์Œ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ฒ„์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์„œ๋ฒ„๋“ค๊ณผ ์ฐจ์ด์ ์€ ๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ๋Š” ์•„ํŒŒ์น˜ ์„œ๋ฒ„๊ฐ€ ์Šค๋ ˆ๋“œ/ํ”„๋กœ์„ธ์Šค ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์— ๋Œ€์กฐ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ตฌ์กฐ๋Š” ์„œ๋ฒ„์— ๋งŽ์€ ๋ถ€ํ•˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์„ฑ๋Šฅ์„ ์˜ˆ์ธกํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

์œ„ํ‚ค๋ฐฑ๊ณผ ์ฐธ์กฐ

https://ko.wikipedia.org/wiki/Nginx

์‚ฌ์šฉ ์ด์œ 

Nginx๋ฅผ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” https ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž…๋‹ˆ๋‹ค. Nginx๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•ด๋‹น ๋ฐฉ๋ฒ•์€ ๋‹ฌ์— 18๋‹ฌ๋Ÿฌ์— ํ•ด๋‹นํ•˜๋Š” ๋น„์šฉ์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋น„์Šค๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ ๋˜๊ฒŒ๋˜๋ฉด ๊ณ ๋ คํ•ด์•ผ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ Nginx๋Š” ๋ฌด๋ฃŒ๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ์— ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Nginx๋ฅผ EC2์— ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

yum install nginx -y
# nginx๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

nginx -v
# ์„ค์น˜๋œ nginx ๋ฒ„์ „ ํ™•์ธ

์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ec2์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. error๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํŠน์ • ๋ช…๋ น์–ด ์‚ฌ์šฉ์„ ์ถ”์ฒœํ•ด์ฃผ๋Š”๋ฐ ํ•ด๋‹น ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Certbot

HTTPS (Hypertext Transfer Protocol Secure) is the update to HTTP that uses the SSL/TLS protocol to provide security for connections between web browsers and web servers. Using HTTPS normally requires a certificate from a certificate authority, such as Letโ€™s Encrypt, and will also require installing that certificate onto a web server. Certbot can help perform both of these steps automatically in many cases. HTTPS is an Internet standard and is normally used with TCP port 443.

from https://certbot.eff.org/pages/help

์œ„ ์ง€๋ฌธ์€ Certbot์—์„œ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค. Https์— ๋Œ€ํ•œ ์„ค๋ช…๋„ ํฌํ•จ๋˜์–ด์žˆ๋Š”๋ฐ, HTTPS๋ž€ HTTP์˜ ์—…๊ทธ๋ ˆ์ด๋“œ ๋ฒ„์ „์œผ๋กœ ์›น ํด๋ผ์ด์–ธํŠธ์™€ ์›น ์„œ๋ฒ„๊ฐ„์— ๋ณด์•ˆ ์—ฐ๊ฒฐ์„ ์ง€์›ํ•˜๊ธฐ์œ„ํ•ด SSL/TLS ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. HTTPS๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Letโ€™s Encrypt ์™€ ๊ฐ™์€ ์ธ์ฆ ๊ธฐ๊ด€์˜ ์ธ์ฆ์„œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์›น ์„œ๋ฒ„๋Š” ์ธ์ฆ์„œ๋ฅผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. Certbot์€ ์ž๋™์œผ๋กœ ์ธ์ฆ์„œ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค. HTTPS๋Š” ์ธํ„ฐ๋„ท ํ‘œ์ค€์ด๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ TCP port 443์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ด์œ 

๋ฌด๋ฃŒ๋กœ SSH ์ธ์ฆ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Certbot์„ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

$ sudo apt-get update
$ sudo apt-get install letsencrypt -y

์œ„์™€๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ec2์˜ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ์ถ”์ฒœ ๋ช…๋ น์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ธ์ฆ์„œ๋ฅผ ์žฌ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ์œ„ํ•ด์„œ nginx๊ฐ€ 80๋ฒˆ ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์ค‘์ง€ ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

$ sudo systemctl stop nginx

์ธ์ฆ์„œ ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ

# sudo letsencrypt certonly --standalone -d [๋„๋ฉ”์ธ ์ž…๋ ฅ] // www.example.com

์ธ์ฆ์„œ ๋ฐœ๊ธ‰์„ ์‹ ์ฒญํ•˜๊ณ  ๋‚˜ํƒ€๋‚˜๋Š” ์ง€๋ฌธ์—์„œ ์ด๋ฉ”์ผ์„ ๋“ฑ๋กํ•˜๊ณ  ์š”๊ตฌ์‚ฌํ•ญ์€ ์ฝ์–ด๋ณด์‹œ๊ณ  y๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์˜ ๊ฒฝ๋กœ์— ์ธ์ฆ์„œ ํ‚ค๋ฅผ ๋ฐ›์€๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

/etc/letsencrypt/live/๋„๋ฉ”์ธ/fullchain.pem
/etc/letsencrypt/live/๋„๋ฉ”์ธ/privkey.pem

Nginx ์„ค์ •ํ•˜๊ธฐ

Nginx๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์„ค์ •ํŒŒ์ผ์ด ๋‹ด๊ธด ์œ„์น˜๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

nginx๋Š” ๋ฃจํŠธ ๊ฒฝ๋กœ์—์„œ etc/nginx ํด๋”์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

ํ•ด๋‹น ๊ฒฝ๋กœ์—์„œ nginx.conf ํŒŒ์ผ์—์„œ server ๋ถ€๋ถ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ธํŒ…ํ•ฉ๋‹ˆ๋‹ค.

server {
    listen       80;
    listen       [::]:80;
    server_name  ๋‚˜์˜๋„๋ฉ”์ธ;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    ssl_certificate /etc/letsencrypt/live/๋‚˜์˜๋„๋ฉ”์ธ/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/๋‚˜์˜๋„๋ฉ”์ธ/privkey.pem;

    server_name ๋‚˜์˜ ๋„๋ฉ”์ธ;

    location / {
                proxy_pass http://localhost:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
      }
}                                                                                                                                                                                 81,0-1        6                                                                                                                                                                                            1,1           Top

์Šคํฌ๋ฆฝํŠธ ํ•ด์„

์œ„ ์Šคํฌ๋ฆฝํŠธ์˜ ์˜๋ฏธ๋Š” http ํ”„๋กœํ† ์ฝœ 80 ํฌํŠธ๋กœ ์ ‘์†ํ•œ ์œ ์ €๋ฅผ https ํ”„๋กœํ† ์ฝœ๋กœ ์žฌ์ ‘์† ์‹œํ‚ค๊ณ  https ํ”„๋กœํ† ์ฝœ 443 ํฌํŠธ๋กœ ์ ‘์†ํ•œ ์œ ์ €์—๊ฒŒ 8080ํฌํŠธ์— ์—ด๋ ค์žˆ๋Š” ์„œ๋ฒ„๋กœ ์ด๋™ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. httpsํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ certbot์œผ๋กœ ์ƒ์„ฑํ•œ pem ํ‚ค๋ฅผ ํ†ตํ•ด ์ธ์ฆ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์œ„์™€๊ฐ™์ด ์„ธํŒ…์ด ๋˜์—ˆ๋‹ค๋ฉด, ์ด์ œ nginx๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Nginx ์‹คํ–‰ ๋ช…๋ น์–ด

๊ธฐ๋ณธ ๋ช…๋ น์–ด

# ์‹œ์ž‘
sudo nginx
# ๋น ๋ฅธ ์ข…๋ฃŒ
sudo nginx -s stop
# ์ •์ƒ ์ข…๋ฃŒ
sudo nginx -s quit 
# ์žฌ์‹œ์ž‘
sudo nginx -s restart
# ๋‹ค์‹œ ๋กœ๋“œ
sudo nginx -s reload

์œ„ ๋ช…๋ น์–ด๋“ค์€ nginx ๊ธฐ๋ณธ ๋ช…๋ น์–ด๋“ค์ด๋‹ค. ํ•˜์ง€๋งŒ ์„œ๋ฒ„๋ฅผ ์žฌ๋ถ€ํŒ… ํ•  ๊ฒฝ์šฐ ๋‹ค์‹œ ์‹œ์ž‘์‹œ์ผœ์ฃผ์–ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์กด์žฌํ•œ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ systemctl์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์„œ๋ฒ„๊ฐ€ ์žฌ๋ถ€ํŒ… ๋˜์–ด๋„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ nginx๋ฅผ ๋‹ค์‹œ ์‹คํ–‰์‹œ์ผœ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

systemctl์„ ์‚ฌ์šฉํ•œ ๋ช…๋ น์–ด

# Nginx ์‹œ์ž‘ ๋ฐ systemctl์— ๋“ฑ๋ก
sudo systemctl start nginx
#Nginx ์ค‘์ง€
sudo systemctl stop nginx
# Nginx ์žฌ์‹œ์ž‘
sudo systemctl restart nginx
# Nginx ๋‹ค์‹œ ๋กœ๋“œ
sudo systemctl reload nginx

์ฐธ์กฐ

https://iter.kr/nginx-์‹œ์ž‘-์ค‘์ง€-์žฌ์‹œ์ž‘-๋ช…๋ น์–ด/

profile
๊ฐœ๋ฐœ์— ์žฌ๋ฏธ๋ฅผ ๋Š๋ผ๋ฉฐ ๊พธ์ค€ํžˆ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž ๊น€์ข…์™„ ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€