ubuntu 22.04에서 Nginx 암호화하기

codeing999·2022년 9월 16일
0

ubuntu

목록 보기
1/2

참고 사이트 : https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-22-04

이 전글에서 이어서 진행

스텝1 - Certbot 설치

certbot이란걸 하기전에 먼저 snap이란걸 최신화해야되나 보다.

sudo snap install core; sudo snap refresh core

예전 버전의 certbot이 있으면 지우라고 한다.

sudo apt remove certbot

난 설치되어있지 않았다고 떴다.
이제 certbot 설치

sudo snap install --classic certbot

그 다음 certbot 명령어를 snap install directory로부터 내 경로로 링크한다고 한다. 환경변수 등록 같은 것 인 듯.

sudo ln -s /snap/bin/certbot /usr/bin/certbot

스텝2 - nginx의 구성 확인하기

우선 이전 글에서 진행한 파일이 있는지 체크. example.com 자리엔 내가 만든 파일 이름으로.
거기에 server_name을 작성했는지 체크하고 없으면 작성하기

sudo nano /etc/nginx/sites-available/example.com

스텝3 - 방화벽을 통해 HTTPS 허용하기

방화벽 현재 상태 보기

sudo ufw status

난 예시화면과 순서만 다르고 똑같이 다음과 같이 결과가 나왔다.

Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)

다음 명령어를 통해 https 트래픽까지 허용? 기존 것은 삭제.

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

다시 상태를 보면

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

이와 같이 바뀌어 있다.

스텝4 - SSL 증명서 얻기

certbot은 플러그인으로 ssl 증면서를 얻는 다양한 방법들을 제공한다. nginx 플러그인은 필요할때마다

sudo certbot --nginx -d example.com -d www.example.com

이 단계에서 난 도메인이 없어서 저번 글에서 ip를 그대로 사용했었는데
ip로 하려니 이런 에러가 나왔다.

Requested name 111.111.111.111 is an IP address. The Let's Encrypt certificate authority will not issue certificates for a bare IP address.

그래서 가비아에서 500원짜리 도메인을 하나 만들었다.
여기엔 가명으로 abc.com로 작성하겠다.
그리고 첫번째 글에 있는 파일 작성하는 부분도 이 도메인을 통하여 다시 진행하였다.

server {

         listen 80;
         listen [::]:80;

    server_name abc.com;

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

}

그리고나서 이제

sudo certbot --nginx -d example.com -d www.example.com

이 명령어를 다시 쳤더니
이메일 입력 등등을 거쳐서 뭐 하더니 실패가 떴다.
www붙인 것에서 에러가 나는 것 같은데
내가 만든 도메인은 www붙인 것까지는 지원하지 않는 모양이다.
가비아에서 설정을 해봤는데 www붙인거랑 안붙인거 동시에 하나만이 같은 ip에 연동된다. (안그래야할거같은데 이상함)
그래서 저건 빼고 명령어를 치기로 했다.

ubuntu@ip-172-31-47-239:/etc/nginx/sites-enabled$ sudo certbot --nginx -d abc.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for abc.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/abc.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/abc.com/privkey.pem
This certificate expires on 2022-12-15.
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 abc.com to /etc/nginx/sites-enabled/abc.com
Congratulations! You have successfully enabled HTTPS on https://abc.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

이와 같이 성공.

증명서가 다운되고 인스톨되고 로드되고 nginx설정이 이제 자동으로 모든 웹 요청을 https://로 리다이렉트 할 것이다.
웹사이트를 리로드하고 브라우저의 보안 인디케이터를 살펴보아라.
분명 lock아이콘과 함께 사이트가 적절히 보안처리되었다고 나타낼 것이다.

여기서 나는 바로는 안들어가졌었는데
문제가 ec2 인스턴스의 인바운드 정책에 443포트를 안열어놨던게 문제였다.
그것을 해결하니 바로 브라우저에서도 https로 들어가지고
프론트 서버에서도 https로 잘 통신이 되었다.

저 certificate를 발급받고 나면 내 abc.com 파일이 다음과 같이 자동으로 바뀌어 있다. (/api만 내가 바꾼거. 안바꿔도 되는데는 상관 없을 거임.)

server {

    server_name abc.com;

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


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/abc.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/abc.com/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 = abc.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



         listen 80;
         listen [::]:80;

    server_name abc.com;
    return 404; # managed by Certbot


profile
코딩 공부 ing..

1개의 댓글