AWS

[AWS] nginx + https 적용하기

Heoky 2022. 2. 21. 16:05

nginx 를 적용하는 이유는 nginx로 next를 리버스 프록시 하기 위함이다.
최초 브라우저에서 프론트로 요청이 오면(80포트) https(443포트)로 변경 후 프론트서버(3060 포트)로 요청을 보낸다.

 


1. ubuntu에서 nginx 설치

ubuntu@ip-172-31-35-68:~/e-Library/prepare/front$ sudo apt-get install nginx

2. nginx configuration 설정

관리자 권한(sudo su)으로 전환 후

ubuntu@ip-172-31-35-68:~/e-Library/prepare/front$ sudo su

vim을 통해 설정 파일에 들어가준다.

ubuntu@ip-172-31-35-68:~/e-Library/prepare/front$ vim /etc/nginx/nginx.conf

설정

http {
...
server {
               server_name coding-factory.site www.coding-factory.site;
               listen 80;
               location / {
                        proxy_set_header Host $host;
                        proxy_pass http://127.0.0.1:3060;
                        proxy_redirect off;
               }
        }
...
}

이후 node 서버가 이미 80번 포트로 돌아가고 있는지 확인한다.

$ sudo lsof -i tcp:80
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
node    1145140 root   18u  IPv4 1750983      0t0  TCP *:http (LISTEN)

돌아가고 있음을 확인 할 수 있다. 서버를 죽이자.

$ sudo kill -9 1145140(PID 번호)

 

3. certbot-auto 설치

만약 이미 wget을 통해 설치한 후 이 에러를 만났다면 반드시 기존에 설치된 certbot을 삭제한 후 snapd를 이용해 설치하자

$ sudo apt-get remove certbot
$ sudo dnf remove certbot
$ sudo yum remove certbot

snapd 설치

$ sudo apt-get update && sudo apt-get install snapd
$ sudo snap install hello-world // 정상적으로 설치되어 작동하는지 확인
$ sudo snap install core; sudo snap refresh core // 최신 버전인가 확인

certbot 설치

$ sudo snap install --classic certbot // certbot 설치
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot // certbot 명령어 실행을 위해 심볼릭 링크
$ sudo certbot --nginx // Certbot edit your Nginx configuration automatically to serve it
$ sudo certbot certonly --nginx // 단순히 인증서만 받아오기

이후 1번 선택 후 인증서 경로를 알고 있어야된다.

Saving debug log to /var/log/letsencrypt/letsencrypt.log

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: coding-factory.site
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1 // 1번 선택

성공 화면 / fullchain.pem과 privkey.pem의 경로 알아두기

Requesting a certificate for coding-factory.site

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/coding-factory.site/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/coding-factory.site/privkey.pem
This certificate expires on 2022-05-22.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for coding-factory.site to /etc/nginx/nginx.conf
Congratulations! You have successfully enabled HTTPS on https://coding-factory.site

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 


이렇게 설치 후 sudo vim /etc/nginx/nginx.conf 가보면

server {
               server_name coding-factory.site;
               location / {
                        proxy_set_header Host $host;
                        proxy_pass http://127.0.0.1:3060;
                        proxy_redirect off;
               }

        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/coding-factory.site/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/coding-factory.site/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

certbot을 통해 인증서 등록이 자동으로 잘 되어있고

server {
                if ($host = coding-factory.site) {
                        return 301 https://$host$request_uri;
                } # managed by Certbot
                        server_name coding-factory.site;
                        listen 80;
                        return 404; # managed by Certbot
        }

80번 포트로 들어왔을 때에도 리버스 프록시 해준다.

 


nginx 재시작

$ sudo systemctl restart nginx