Nginx와 CertBot을 이용한 SSL 환경 구축

 


해당 서버에 Nginx가 설치 되어 있어야 한다. 아래글 참고

Nginx 설치 - Ubuntu


네트워크 구성

IpTime 공유기 1대 (모델 : T5008SE)

리버스 프록시 서버 1대 -01서버 (모델 : JECS-N5095B, OS : Ubuntu 22.04, Nginx설치)

서버 1대 - 02서버 (모델 : JECS-8250B-i5, OS : Ubuntu 20.04)



1. 공유기 포트포워딩은 01서버로 지정

TCP 80 -> 01서버 : 80

TCP 443 -> 01서버 : 443



2. 도메인 DNS는 아래와 같이 진행 (저는 AWS의 Route53사용)

example.com -> 01서버 IP. A레코드(www포함)


02서버는 3000포트가 서비스 포트이다. 따라서 Nginx가 프록시 요청을 받을시 02서버의 3000포트로 연결해 줘야 한다.


3-1. CertBot을 설치한다.


apt 업데이트

$ sudo apt update


snapd 설치

$ sudo apt install -y snapd


코어 설치 및 새로고침

$ sudo snap install core; sudo snap refresh core


Snap를 이용해서 certbot 설치

$ sudo snap install --classic certbot


"/usr/bin/certbot" -> "/snap/bin/certbot"으로 바로가기(심볼릭 링크) 설정

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


certbot 버전 확인 (사용버전 : 5.2.2)

$ certbot --version



4. 01서버를 통해서 SSL인증을 하기 위해서는 인증서가 필요하다.


먼저 인증을 받기위한 Nginx conf파일을 생성 - 도메인 증명을 위한 80 port 사용


파일경로 : /etc/nginx/conf.d/example-http.conf


server {

  listen 80;

  server_name example.com www.example.com;

  location /.well-known/acme-challenge/ { root /var/www/certbot; }

  location / { return 301 https://$host$request_uri; }

}


5. CertBot을 통해서 인증서를 발급받는다.


CertBot 터미널을 이용해서 인증서(example.com)을 발급

$ sudo certbot certonly --webroot -w /var/www/certbot -d example.com -d www.example.com



6. 01서버에 있는 SSL인증서를 이용해서 라우팅


Nginx conf파일을 생성 - 도메인 연결시 HTTPS접속을 위해 443 port사용


파일경로 : /etc/nginx/conf.d/example-https.conf

server {

  listen 443 ssl;

  http2 on;

  server_name example.com www.example.com;

  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;

  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;


  location / {

    proxy_pass http://192.168.0.3:3000;

    proxy_set_header Host $host;

    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

}


7. Nginx를 재시작


conf파일 적용을 위해 Nginx을 재시작

$ sudo nginx -s reload


해당 작업이 완료가 됬다면 HTTPS로 접속이 가능해 집니다.



댓글

이 블로그의 인기 게시물

Lesson 12_1 프로퍼티 노드(Property Node)

DAQ로 전압 측정하기-2

Lesson 12_2 참조를 이용한 프로퍼티노드(Property Node)