Nginx 로 React https 인증서 적용

Qnoze·2024년 1월 5일

Front-End

목록 보기
2/8

Nginx 설치

서버에서 사용할 nginx 를 설치한다.

sudo apt install nginx

certbot 설치

인증서를 받기 위한 certbot을 설치한다.

sudo apt install certbot

인증서 발급받기

certbot 명령어를 이용하여 인증서를 받는다.

sudo certbot --nginx

만약 에러가 뜬다면

만약 certbot에서

Saving debug log to /var/log/letsencrypt/letsencrypt.log
The requested nginx plugin does not appear to be installed

이런 에러가 뜬다면

sudo apt update
sudo apt install python3-certbot-nginx

이 명령어를 순서대로!

nginx 설정 파일 customizing

이 부분은 인증서를 발급받으면서 자동으로 세팅이 되는 곳이니까 참고로만 봐주길!

인증서 사용을 위해 /etc/nginx/nginx.conf 에 설정을 추가로 해줘야 한다.
이 때 유의해야 할 부분은

  • server_name [사용할 도메인 작성]
  • listen : 감시할 포트 작성
  • location : / 가 포함되는 path로 접근하는 경우 감지
  • proxy_pass : 대신 보여줄 것을 적는 곳

이다.

sudo vim /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        server {
                # 도메인을 적는 부분
                server_name jslog.co.kr;

                # 감시할 port
                listen 443 ssl; # managed by Certbot
                ssl_certificate /etc/letsencrypt/live/jslog.co.kr/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/jslog.co.kr/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

                # 접근할 경로
                location / {
                        proxy_set_header HOST $host;
                        # proxy_pass "https로 접근이 오면 보여줄 포트";
                        proxy_pass http://localhost:3050;  # 여기에 현재 사용하는 포트 작성
                        proxy_redirect off;
                }
        }

        server {
                if ($host = jslog.co.kr) {
                        return 301 https://$host$request_uri;
                } # managed by Certbot

                server_name jslog.co.kr;
                listen 80;

                location / {
                        proxy_set_header HOST $host;
                        # proxy_pass "https로 접근이 오면 보여줄 포트";
                        proxy_pass http://localhost:3050;  # 여기에 현재 사용하는 포트 작성
                        proxy_redirect off;
                }

                return 404; # managed by Certbot
        }

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        # include /etc/nginx/conf.d/*.conf;
        # include /etc/nginx/sites-enabled/*;
}

nginx 실행 (명령어들)

# 중지
sudo systemctl stop nginx

# 시작
sudo systemctl start nginx

# 다시 시작
sudo systemctl restart nginx

# 설정 파일 문법 확인
sudo nginx -t

Nginx에 대하여

경량화된 소프트웨어 웹 서버이다.
Nginx 는 싱글 쓰레드로 동작하며 비동기 non-blocking I/O 이벤트 기반으로 요청을 처리한다고 한다. (무슨 말일까..) 그래서 적은 자원으로 효율적인 트래픽 처리가 가능하다고 한다.

Certbot에 대하여

무료로 https 인증서를 발급받을 수 있는 곳이 있는데, certbot를 설치하고 명령어를 검색하면 쉽게 인증서를 발급받을 수 있다고 한다.

발급 에러가 발생한다면..!

  • 포트포워딩이 안되어 있어서 let's encrypt 에서 주소를 찾지 못하여 ssl 인증서를 발급 못해주는 상황일 수도 있으니, 외부에서 도메인 주소가 접속이 가능한지 확인해봐야 한다!

0개의 댓글