Nginx ๋?
Nginx
๋ ์น์๋ฒ์ ์ผ์ข
์ด๋ค.
- https
- redirecting
- ์ ์ ํ์ผ ์ ๊ณต
- ์บ์ฑ
Nginx ์ค์น
$ sudo apt-get update
$ sudo apt-get install nginx
์ค๋นํด๋ EC2 ๋ด๋ถ์์ Nginx
๋ฅผ ์ค์นํด์ฃผ์.
๋๋ฉ์ธ ์ค์
$ sudo vi /etc/nginx/sites-available/api.gongcheck.day
๊ฐ๋น์ ๋ฑ์์ ๊ตฌ๋งคํ ๋๋ฉ์ธ์ ํด๋น ์๋ฒ์ ์ฐ๊ฒฐํด์ค ๊ฒ์ด๋ค.
/etc/nginx/sites-available/default
๋ฅผ ์ฌ์ฉํด๋ ๋์ง๋ง, ํด๋น ์๋ฒ๋ก ์ฌ๋ฌ ๋๋ฉ์ธ์ ๊ด๋ฆฌํ ์๋ ์๊ธฐ ๋๋ฌธ์ ๊ฐ๋ณ ํ์ผ์ ๋ง๋ค์ด ์ฃผ์.
server {
listen 80;
listen [::]:80;
root /var/api/gongcheck.day/html;
index index.html index.htm index.nginx-debian.html;
server_name api.gongcheck.day;
location / {
try_files $uri $uri/ =404;
}
}
SSL ์ธ์ฆ์๋ฅผ ๋ฐ๊ธ ๋ฐ๊ธฐ ์ํด ํด๋น ํ์ผ์ ์์ ๊ฐ์ ์๋ฒ ๋ธ๋ก์ ์์ฑํด์ฃผ์.
listen
์ 80 ํฌํธ๋ก HTTP ํต์ ์ ๋ฐ๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.root
๋ ์ด ๋๋ฉ์ธ์ผ๋ก ์ ์ํ ๊ฒฝ์ฐ ํ์ผ ๊ฒฝ๋ก์ด๋ค.index
๋ ๊ธฐ๋ณธ๊ฒฝ๋ก๋ก ์ ์ํ ๊ฒฝ์ฐ ๊ฐ์ฅ ๋จผ์ ๋ณด์ฌ์ง ํ์ผ์ด๋ค.server_name
์ ๊ตฌ๋งคํ ๋๋ฉ์ธ๋ช ์ ์์ฑํด์ฃผ์.
ํ์ผ ๋๊ธฐํ
$ sudo ln -s /etc/nginx/sites-available/api.gongcheck.day /etc/nginx/sites-enabled
sites-available
์ ์์ฑํ ํ์ผ์ ์ ์ฉํ๊ธฐ ์ํด sites-enabled
์ ๊ฐ์ ํ์ผ์ ์์ฑํด์ผํ๋ค.
์ํํธ๋งํฌ๋ฅผ ํตํด ๋ ํ์ผ์ ์ฐ๊ฒฐํด์ฃผ๋๋ก ํ์.
Nginx ํ ์คํธ
$ sudo nginx -t
$ sudo service nginx restart
Nginx
ํ
์คํธ๋ฅผ ํตํด ์๋ฒ ๋ธ๋ก ์คํ ๋ฑ์ ์ฐพ์๋ผ ์ ์๋ค. ํ
์คํธ ์ดํ ์ฌ์์ํด์ฃผ์.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
์ด๋ฐ ๊ฒฐ๊ณผ๊ฐ ๋ณด์ธ๋ค๋ฉด ํ ์คํธ์ ์ฑ๊ณตํ๋ค๋ ์๋ฏธ์ด๋ค.
CertBot
CertBot ์ด๋?
CertBot
์ Letโs Encrypt ์ SSL ์ธ์ฆ์๋ฅผ ์ฝ๊ฒ ๋ฐ๊ธ ๋ฐ์ ์ ์๋๋ก ๋์์ฃผ๋ ํ๋ก๊ทธ๋จ์ด๋ค.
Letโs Encrypt ์์ ๋ฐ๊ธ ๋ฐ์ SSL ์ธ์ฆ์๋ฅผ ํตํด ๋๋ฉ์ธ์ HTTPS ๋ฅผ ์ ์ฉํ ์ ์๋ค.
CertBot ์ค์น
$ sudo apt-get install python3-certbot-nginx
๋๋ฉ์ธ ์ธ์ฆ์ ๋ฐ๊ธ
$ sudo certbot --nginx -d api.gongcheck.day
๋ฐ๊ธ์ด ์ฑ๊ณต์ ์ผ๋ก ์ด๋ฃจ์ด์ก๋ค๋ฉด sites-available
์ ์์ฑํ๋ ํ์ผ์ด ๋ณ๊ฒฝ๋์ด ์์ ๊ฒ์ด๋ค.
๋ฆฌ๋ฒ์ค ํ๋ก์ ์ค์
server {
server_name api.gongcheck.day;
location / {
proxy_pass http://192.168.1.199:8080/index.html;
}
location ^~ /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
proxy_pass http://192.168.1.199:8080; # ๊ฐ๋ฐ ์๋ฒ Private IP
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.gongcheck.day/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.gongcheck.day/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 {
if ($host = api.gongcheck.day) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name api.gongcheck.day;
return 404; # managed by Certbot
}
server
ํ์ location
๋ด๋ถ์ ์์ ๊ฐ์ ์ฝ๋๋ฅผ ์ถ๊ฐํ์ฌ ํ๋ก์ ์ค์ ์ ํด์ฃผ์.
์ด๋ฅผ ํตํด ํด๋น ๋๋ฉ์ธ์ผ๋ก ๋ค์ด์จ ์์ฒญ์ ๋ชจ๋ ๋ฐฑ์๋ ์๋ฒ๋ก ๋ณด๋ผ ์ ์๋ค.