nginx에 SSL(HTTPS) 적용하기

Kim Ji Eun·2021년 6월 10일
1

전 포스팅에서 도메인을 nginx에 연동해보았습니다.
이번 포스팅에서는 도메인에 ssl/https를 적용해보려고 합니다.

1. Certbot 설치하기

  • Certbot은 Let’s Encrypt 인증서를 자동으로 발급 및 갱신을 해주는 봇 프로그램입니다.
# sudo su #/var/www/html 안에서 
# apt-get update
# apt-get install software-properties-common
# add-apt-repository universe
# add-apt-repository ppa:certbot/certbot
# apt-get update

# apt install certbot
# apt install python-certbot-nginx -> nginx와 사용할때 

# certbot --nginx

(앞으로 나오는 질문들에 대한 응답 모음)

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

Plugins selected: Authenticator nginx, Installer nginx

Enter email address (used for urgent renewal and security notices) (Enter 'c' to

cancel): info@hometek.co.kr

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at

[https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf.](https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf.) You must

agree in order to register with the ACME server at

https://acme-v02.api.letsencrypt.org/directory

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier

Foundation, a founding partner of the Let's Encrypt project and the non-profit

organization that develops Certbot? We'd like to send you email about our work

encrypting the web, EFF news, campaigns, and ways to support digital freedom.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

No names were found in your configuration files. Please enter in your domain

name(s) (comma and/or space separated)  (Enter 'c' to cancel): visithome.shop www.visithome.shop

Obtaining a new certificate

Performing the following challenges:

http-01 challenge for visithome.shop

http-01 challenge for www.visithome.shop

Waiting for verification...

Cleaning up challenges

Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default

Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/default

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.

2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for

new sites, or if you're confident your site works on HTTPS. You can undo this

change by editing your web server's configuration.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/default

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/default

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://visithome.shop and

https://www.visithome.shop

You should test your configuration at:

https://www.ssllabs.com/ssltest/analyze.html?d=visithome.shop

https://www.ssllabs.com/ssltest/analyze.html?d=www.visithome.shop

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:

- Congratulations! Your certificate and chain have been saved at:

/etc/letsencrypt/live/visithome.shop/fullchain.pem

Your key file has been saved at:

/etc/letsencrypt/live/visithome.shop/privkey.pem

Your cert will expire on 2020-01-25. 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"

- Your account credentials have been saved in your Certbot

configuration directory at /etc/letsencrypt. You should make a

secure backup of this folder now. This configuration directory will

also contain certificates and private keys obtained by Certbot so

making regular backups of this folder is ideal.

- 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

완료되면 아래와 같이 뜹니다.

2. 미래에 인증서 갱신을 해주기 위해 "certonly" 옵션을 주고 다시 한번 실행합니다.

# certbot certonly --nginx

(앞으로 나오는 질문들에 대한 응답 모음 )

manager@manager-PC:~$ sudo certbot certonly --apache

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

Plugins selected: Authenticator apache, Installer apache

Which names would you like to activate HTTPS for?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: visithome.shop

2: www.visithome.shop

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input

blank to select all options shown (Enter 'c' to cancel): 1 2

Cert not yet due for renewal

You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.

(ref: /etc/letsencrypt/renewal/visithome.shop.conf)

What would you like to do?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Keep the existing certificate for now

2: Renew & replace the cert (limit ~5 per 7 days)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Renewing an existing certificate

IMPORTANT NOTES:

- Congratulations! Your certificate and chain have been saved at:

/etc/letsencrypt/live/visithome.shop/fullchain.pem

Your key file has been saved at:

/etc/letsencrypt/live/visithome.shop/privkey.pem

Your cert will expire on 2020-01-25. To obtain a new or tweaked

version of this certificate in the future, simply run certbot

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

3. 자동갱신

유효 기간이 3개월이므로 3개월 마다 자동 갱신을 해주어야합니다.

만료 날짜에 신경쓰기 싫을때는 매일 갱신하도록 /etc/crontab에 다음과 같이 추가합니다.

(매일 0시 0분에 실행함. 정확한 만료 날짜를 알면 정확하게 지정.)

# vi /etc/crontab
0  0 * * * root certbot -q renew --nginx >> /var/log/letscript.renewal.log 2>&1 #추가
# service cron restart

4. 설정파일 자동수정

설치된 인증서는 /etc/letsencrypt/live/lilyspace.shop에서 확인할 수 있습니다.

실행 결과 nginx의 환경파일(vi /etc/nginx/sites-available/defaut)이 자동 수정됩니다.

아래의 내용이 추가됨을 확인할 수 있습니다.

5. 설정파일에 프록시 패스 추가

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                proxy_pass http://localhost:8080;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;

        }

6. https 적용 확인

profile
Back-End Developer

0개의 댓글