티스토리 뷰

반응형

Let's Encrypt를 통하여 Nginx에서 무료로 https 적용하기

Let’s Encrypt를 통해 Nginx에 무료로 HTTP를 적용해보겠습니다. Let’s Encrypt는 SSL 인증서를 받을 수 있는 CA(Certificate Authorities) 기간중 하나입니다. Let’s Encrypt는 무료로 SSL인증서를 발급해줍니다. 여러 글로벌 기업들로부터 후원을 받고 있으며 신뢰할 수 있는 인증 기관(Trusted CA)으로 인증받은 CA입니다.

Let’s Encrypt의 SSL인증서를 사용하기 위해서는 ACME(Automatic Certificate Management Environment) 프로토콜을 준수하는 프로그램을 사용해야합니다. Let’s Encyrpt에서는 Certbot 프로그램을 사용하는것을 궈장합니다.

Certbot을 개발자가 서버에서 실행하여 인증서를 발급받는 프로그램입니다. 개발자는 Certbot을 통해 인증서를 직접 발급받아 Nginx에 적용시킬 수 있습니다. 다양한 운영체제와 웹서베에서 다운받을수 있게 다양한 패키를 제공하고 있습니다. 먼저 Certbot을 다운받아보도록 하겠습니다. 환경은 Centos 환경입니다.

1. Certbot 설치

아래의 명령어를 실행시켜 Certbot을 다운을 받아야합니다.

$ curl -O https://dl.eff.org/certbot-auto
$ chmod +x certbot-auto
$ sudo mv certbot-auto /usr/local/bin/certbot-auto

그다음 다운받은 Certbot을 통해 SSL 인증서를 발급받습니다. 발급 방법은 간단합니다.

2. SSL 인증 발급 받기

다음은 아래 명령어를 통해 인증서를 획득합니다. 아래 옵션을 Standalone으로 가져갔는데 이 방식은 certbot 에서 80 포트의 standalone webserver 를 따로 띄워서 인증서를 발급받는 방식입니다.

certbot-auto certonly --standalone -d example.com --debug

인증서 발급 위치는 /etc/letsencrypt/live/{도메인명}에 발급 됩니다. 해당 경로에 가게되면 4개의 pem파일들이 생성되는것을 볼 수 있습니다.

  1. cert.pem: 도메인 인증서
  2. chain.pem: Let’s Encrypt chain 인증서
  3. fullchain.pem: cert.pem + chain.pem
  4. privkey.pem: 인증서의 개인키

여기에서 발급된 키를 nginx에 설정하게되면 https적용이 됩니다.

3. Nginx 설정

그런다음 위에서 발급받은 키를 기반으로 Nginx에 SSL을 적용하기 위해서는 아래와같이 nginx 를 설정해줍니다. ssl_certificate, ssl_certificate_key의 경로는 각 환경에 맞게 커스텀하여 사용해야합니다.

server {
    listen 80;
    return 301 https://$host$request_uri; // http를 https로 redirect
}

server {

        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;

        server_name  localhost;
        root         /usr/share/nginx/html;

         ssl_certificate "/etc/letsencrypt/live/example.com/fullchain.pem";
         ssl_certificate_key "/etc/letsencrypt/live/example.com/privkey.pem";
         # It is *strongly* recommended to generate unique DH parameters
         # Generate them with: openssl dhparam -out /etc/pki/nginx/dhparams.pem 2048
         #ssl_dhparam "/etc/pki/nginx/dhparams.pem";
         ssl_session_cache shared:SSL:1m;
         ssl_session_timeout  10m;
         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
         ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
         ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                root /usr/share/nginx/html;
                index  index.html index.htm;
        }
}

위와같이 설정한 후 Nginx를 재 실행한 후 https를 적용하여 사이트에 접속하면 성공적으로 접속 되는것을 볼 수 있습니다. HTTPS 설정에 따라 보안등급을 확인하고 싶다면 https://www.ssllabs.com/ssltest/ 사이트에 접속해서 호스팅 정보를 입력하면 보안등급을 확인할 수 있습니다.

Let’s encrypt 인증서로 발급받은 인증서는 3개월 만기기 때문에 이를 반복해서 갱신해야 합니다. 아래 명령어로 인증서 갱신을 할 수 있습니다.

$ certbot-auto renew
반응형

'Infra' 카테고리의 다른 글

Mac 에서 Jenkins 설치하기  (0) 2020.06.19
SSH key 를 통해 비밀번호 없이 원격 접속  (0) 2020.05.27
Jenkins 에 Github ssh 연동하기  (0) 2019.11.16
Jenkins 설치방법  (0) 2019.11.04
AWS EC2(Amazon Linux) Java 8 설치하기  (0) 2019.11.02
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함