✨ Nginx 를 사용해야하는 이유 1(리버스 프록시)

일반적으로 Http 요청80번 포트를, Https 요청443번 포트를 사용한다.

그러나, 위 포트로 서버를 여는 것은 보안상 그리 좋은 판단은 아니다.

그냥 80번 포트로 Node.js 서버를 열면 안되나요?

Nest.js나 다른 프레임워크를 사용해본 분들 이라면 3000번 포트가 굉장히 익숙할 것이다. Watchducks의 프록시 서버도 마찬가지로 3000번 포트로 열려있다. 그렇다면, 어떻게 Http와 Https요청을 서버에게 알릴 수 있을까?

Nginx에는 리버스 프록시라는 기능이 탑재되어있다. Watchducks서비스 또한 리버스 프록시의 일종이라고 할 수 있는데 외부로부터 요청을 특정 목적지로 전달 혹은 특정 목적지로 전달되는 요청을 중간에서 중개한다.

여기서는 80번3000번 포트로 오는 Http, Https요청을 3000번 포트로 우회시켜준다. 따라서, 우리는 정상적으로 3000번 포트에서 Http, Https요청을 처리할 수 있게 된다.

✨ Nginx 를 사용해야하는 이유 2(SSL/TLS 터미네이션)

Https는 🔗TLS 라는 보안 프로토콜을 사용해서 통신하게 된다. 이를 서버에서 직접 구현하려면 꽤나 복잡하기도 하고 관리 포인트가 늘어나기 때문에 Nginx에서 서버로 요청을 우회하기 전에 처리해주게 된다.

그 전에 TLS 인증서를 등록하는 과정이 필요하다. 인증서를 등록 서비스를 제공하는 제 3자 서비스인 🔗Certbot을 사용한다. Certbot은 Nginx에서 조금 복잡하지만 간단한 설정을 통해 TLS 터미네이션을 구현할 수 있다.

참고로 도메인 네임이 있어야 한다

Nginx, Certbot 설치

sudo apt install nginx # nginx 설치
snap version # snap 설치 확인 없다면 sudo apt install snap
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot # 명령어 심볼릭 링크 생성

이제 Nginx설정에서 TLS 터미네이션 설정을 추가하자.

Nginx 설정

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

events {
    worker_connections 768;
}

http {
    # Basic Settings
    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;

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

    # SSL Settings
    ssl_protocols TLSv1.2 TLSv1.3;
    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;

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

설정을 완료했다면 아래 명령어로 Nginx를 테스트할 수 있다.

sudo nginx -t
# output
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful