http
를 사용하면 보안에 대한 이슈가 많이 생겨 요즘은 SSL
을 적용시켜 https
를 사용하는 것은 필수이다. FE 및 BE에 각각 적용을 시키는 것보다 앞단의 nginx
에서 SSL
을 처리를 해주면 더 간단하게 적용 할 수 있을 거 같아 작업을 해보았다.
nginx
가 설치된 서버- 서버에 도메인이 연결되어있어야함
- NHN Cloud Instance (서버)
- NHN Cloud DNS Plus (DNS 연동)
- Ubuntu Server 20.04 LTS
- nginx 1.18.0 (Ubuntu)
Let's Encrypt
는 사용자에게 무료로 TLS 인증서를 발급해주는 비영리기관이다.
이 글에서는 https://letsencrypt.org/ko/getting-started 에서 제공하는 Shell Access
권한이 있을 경우에 사용하는 certbot client
를 이용하여 작업을 하였다.
certbot
은 자동으로 인증서를 발급받고 관리 및 갱신해주는 프로그램이다.
https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal 에서 nginx와 ubuntu를 선택하여 설치방법을 따라하였다.
certbot 사이트에 나와있는 설치방법
- SSH into the server
- Install snapd
- Ensure that your version of snapd is up to date
- Remove certbot-auto and any Certbot OS packages
- Install Certbot
- Prepare the Certbot command
- Choose how you'd like to run Certbot
- Test automatic renewal
- Confirm that Certbot worked
sudo apt update
sudo apt-get install snapd
sudo snap install core; sudo snap refresh core
sudo apt-get remove certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
ubuntu 기본 설정을 따라 nginx 설정파일을 연다.
sudo vi /etc/nginx/sites-available/default
server_name의 example.com을 자신의 도메인에 맞게 설정하면 된다.
이 서버는 FE의 배포 서버이므로 아래와 같이 location을 설정하였다.
server {
listen 80;
server_name example.com;
location / {
root /home/ubuntu/Project/Front/build;
index index.html index.htm;
try_files $uri /index.html;
}
}
위와 같이 default 파일을 변경한 후 :wq
를 사용하여 저장한다.
sudo certbot --nginx
위 명령어를 실행하면 이메일을 적어주는 것이 나온 후 약관을 묻는다.
이메일을 적어주고 동의하자.
그 이후 자신이 사용할 도메인을 선택하거나 입력해주면 작업이 완료된다.
Congratulations! 가 출력되면 https가 적용된 것이다.
sudo systemctl restart nginx
nginx를 재시작해주면 https
가 잘 작동하는 것을 볼 수 있다.
원래는 nginx 설정내에서 80
포트를 443
포트로 redirect
해주는 코드가 필요하지만 certbot
이 알아서 default
파일을 아래와 같이 바꾸어 놓으니 따로 작업을 안해주어도 된다.
# sudo vi /etc/nginx/sites-available/default
server {
server_name example.com;
location / {
root /home/ubuntu/Project/Front/build;
index index.html index.htm;
try_files $uri /index.html;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.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 = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.com;
return 404; # managed by Certbot
}
이번 포스트에서는 FE 배포 서버를 기준으로 하였으나 BE API 서버는 nginx의 프록시 서버를 이용하여 동일한 과정을 진행하면 가능하다.
잘못된 내용이 있으면 댓글에 남겨주시면 감사하겠습니다🥺