Skip to content

Instantly share code, notes, and snippets.

@woorim960
Created January 9, 2022 02:50
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save woorim960/dda0bc85599f61a025bb8ac471dfaf7a to your computer and use it in GitHub Desktop.
Save woorim960/dda0bc85599f61a025bb8ac471dfaf7a to your computer and use it in GitHub Desktop.
Nginx를 이용하여 https 적용하는 법

Nginx 설치

# Nginx 설치
$ sudo apt install nginx

# Nginx 실행
$ sudo service nginx start

이로써, Nginx 설치가 끝났다.


https를 적용하기 위해서는 certbot을 설치해야한다.

  1. snapd를 이용하여 certbot 설치 아래 명령으로도 certbot을 설치할 수 있으나, Ubuntu 최신 버전(v20)에서는 동작하지 않았다.
$ wget https://dl.eff.org/certbot-auto

따라서, snapd를 이용하여 설치해줘야 한다. 아래 명령을 실행해주도록 하자.

# snap을 이용하여 core 설치 -> snap을 최신 버전으로 유지하기 위해 설치
$ sudo snap install core

# core를 refresh 해준다.
$ sudo snap refresh core

# 기존에 잘못된 certbot이 설치되어있을 수도 있으니 삭제 해준다.
$ sudo apt remove certbot

# certbot 설치
$ sudo snap install --classic certbot

# certbot 명령을 로컬에서 실행할 수 있도록 snap의 certbot 파일을 로컬의 cerbot과 링크(연결) 시켜준다. -s 옵션은 심볼릭링크를 하겠다는 것.
$ ln -s /snap/bin/certbot /usr/bin/certbot

  1. certbot을 이용해 ssl 인증서를 받아온 뒤 cerbot이 스스로 nginx설정을 해주도록한다. 아래 명령을 수행하면 여러 질문이 등장한다. 질문에 맞춰 답을 해주면 된다. domain을 입력하라는 질문에서는 본인이 사용할 Domain을 입력해주면 된다. 필자 같은 경우는 프로젝트의 도메인이 idu-market.shop이므로 이것으로 해줬다.
$ sudo certbot --nginx

위 명령을 잘 수행하고 아래처럼 뜨면 잘 된 것이다.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Your existing certificate has been successfully renewed, and the new certificate
has been installed.

The new certificate covers the following domains: https://idu-market.shop # https가 설정된 도메인을 알려준 것.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Subscribe to the EFF mailing list (email: woorimprog@gmail.com).

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/idu-market.shop/fullchain.pem # 공개키 경로이므로 기억해두자.
   Your key file has been saved at:
   /etc/letsencrypt/live/idu-market.shop/privkey.pem # 비밀키 경로이므로 기억해두자.
   Your certificate will expire on 2021-08-15. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again with the "certonly" option. To non-interactively
   renew *all* of your certificates, run "certbot renew"
 - 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

위 출력을 잘보면 https가 적용된 도메인과 공개키와 비밀키의 경로를 알려준다. 주석으로 처리해뒀으나 다시 한 번 정리하자면 아래와 같다.

1. https가 설정된 도메인
https://idu-market.shop

2. 공개키 경로
/etc/letsencrypt/live/idu-market.shop/fullchain.pem

3. 비밀키 경로
 /etc/letsencrypt/live/idu-market.shop/privkey.pem

여기까지 설정을 마쳤다면, certbot이 알아서 Nginx 설정까지 해줬기 때문에 https 설정도 되어있을 것이다. 필자의 경우는 아래처럼 설정이 되었다.

# 443 포트로 접근시 ssl을 적용한 뒤 3000포트로 요청을 전달해주도록 하는 설정.
server {
        server_name idu-market.shop;

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

        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/idu-market.shop/fullchain.pem; # managed by Cert>
        ssl_certificate_key /etc/letsencrypt/live/idu-market.shop/privkey.pem; # managed by Ce>

        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

# 80 포트로 접근시 443 포트로 리다이렉트 시켜주는 설정
server {
        if ($host = idu-market.shop) {
                return 301 https://$host$request_uri;
        } # managed by Certbot


        listen 80;
        server_name idu-market.shop;
        return 404; # managed by Certbot
}

보충

  • certbot을 이용하여 ssl인증서를 발급할 경우 3개월 마다 갱신을 해줘야 한다. -> 무료이기 때문에 안전하게 서비스를 이용하려면 갱신해야함. 아래 명령을 통해 갱신해줄 수 있다.
$ certbot renew

인증서의 유효기간이 끝나가면 본인이 certbot을 통해 ssl인증서를 받아올 때 입력해 준 이메일로 알림이 오게된다. 해당 메일을 받으면 위 명령을 통해 갱신해주면 된다.


참고자료

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment