본문 바로가기

서버

ec2) amazon linux - nginx에 ssl적용하기

728x90

사전작업

- 도메인 구매(가비아)

- aws ec2의 dns 정보를 가비아에 설정

- aws 보안그룹에 443포트 열어놓기

- 서버에 nginx 설치

 

 


 

1. certbot 설치

sudo yum install certbot
sudo yum install python-certbot-nginx

 

2. certbot으로 ssl발급하기 (example.com을 도메인 이름으로 가정함)

sudo certbot --nginx -d example.com

 

 

3. nginx.conf 파일 수정하기 (etc/nginx/nginx.conf )

※ 이건 원본파일에 바로 작업하는 경우임, 원래 심볼링링크 파일로 작업하는 경우가 많음. 

원본파일을 건드리고 싶지 않은 경우는 본 포스팅을 참고하면 안됨

더보기

 

#...중략
#server 영역

    server {
        listen       80;
        listen       [::]:80;
        server_name  도메인명;
        root         /usr/share/nginx/html;
        location / {
            proxy_pass http://localhost:3000;
#           proxy_http_version 1.1;
#           proxy_set_header Upgrade $http_upgrade;
#           proxy_set_header Connection 'upgrade';
#           proxy_set_header Host $host;
#           proxy_cache_bypass $http_upgrade;
        }

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

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

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




                listen       80;
                listen       [::]:80;
                server_name 도메인명;
            return 404; # managed by Certbot


}

 

4. 서버를 재시작하기

sudo service nginx restart

 

5. 이상 없는지 확인

sudo nginx -t

 

 

도메인으로

들어가보면 적용되어 있음