본문 바로가기
리눅스

Nginx SSL/TLS ssl_preread & stream

by Justin입니다. 2025. 3. 17.

Rocky Linux8에서 디폴트 설치 Nginx 버전은 1.14 버전이다. 하지만 ssl_preread 기능은 Nginx 1.22 버전이상에서 지원됨으로 필요한 버전에 맞게 설치해야한다.

 

Nginx 모듈 리스트 확인 

현재 Nginx 모듈 스트림을 확인 할 수 있다. 

Nginx 1.22 버전을 설치하기위해 아래의 명령어로 활성화 한다 .

dnf module enable nginx:1.22

 

 

1.14 버전이d (disable)상태이고 1.22 버전이 새롭게 (enable) 상태가 되었다 .

# 1.22 버전 nginx 설치
sudo dnf module install nginx:1.22

설치 완료

 

 

Nginx 에서 stream 디렉티브는 HTTP 프로토콜 외의 TCP와 UDP 프로토콜을 처리하기 위한 블록이다. 

이를 통해 웹 애플리케이션 서버 외에도 데이터베이스, 메일 서버, 게임 서버와 같은 다른 네트워크 서비스의 로드밸런싱을 효율적으로 할 수 있다.

Stream 디렉티브는 nginx의 메인 설정 블록 내에서 사용된다. 예를 들어 TCP 프로토콜을 사용하는 서비스를 로드밸런싱 하려면 아래와 같은 설정을 사용할 수 있다. 

stream {
    upstream myapp {
        server backend1.example.com:12345;
        server backend2.example.com:12345;
    }

    server {
        listen 12345;
        
        proxy_pass myapp;
    }
}

stream 블록은 upstream과 server 디렉티브를 사용하여 네트워크 프로토콜을 처리한다. 

upstream 은 트래픽을 전달할 백엔드 서버들을 정의한다. 여기서는 두개의 백엔드 서버를 설정했다.

server 블록은 listen 지시어를 사용하여 특정포트를 리스닝하고, 들어오는 트래픽을 proxy_pass를 통해 upstream 으로 전달한다.

 

 

stream 구문이 들어간 nginx.conf 파일을 실행하면 아래와 같은 오류 발생

journalctl -xe

stream directive 를 인식할 수 없다는 오류

nginx -V 명령어로 stream 옵션이 포함되있는지 확인

이럴 경우에는 nginx.conf 최상단에 stream 모듈을 직접 추가해주어야함.

 

 

ngx_stream_module.so 경로 추적 및 nginx.conf 에 추가

find / -name "ngx_stream_module.so"
-> /usr/lib64/nginx/modules/ngx_stream_module.so

 

nginx.conf 최상단에 모듈 추가

 

 

nginx 정상 구동 확인

 

 

/etc/nginx/nginx.conf

# TCP/UDP 스트림 관련 설정
stream {

    # /etc/nginx/stream.d/ 디렉터리의 모든 .conf 파일 포함 
    include /etc/nginx/stream.d/*.conf; # Stream 설정 포함
}

 

 

/etc/nginx/stream.d/proxy.conf

server {
    listen 3003;
    proxy_pass hyukjin_store;
    ssl_preread on;
}

 

 

/etc/nginx/stream.d/upstreams.conf

upstream hyukjin_store {
    server 10.10.44.5:80;
}

 

 

/etc/nginx/stream.d/stream.map.conf

map $ssl_preread_server_name $upstream_name {
    default hyukjin_store; # (SNI 없는 요청 처리)
    hyukjin.store hyukjin_store;
}

요청 도메인이 hyukjin.store 는 hyukjin_store 로 전달된다

 

 

 

파일이 흩어져 있지만 합치면 아래와 같다

# TCP/UDP 스트림 관련 설정
stream {
    # 해시 테이블 크기 설정
    map_hash_bucket_size 64;
    # 스트림 관련 에러 로그 설정
    error_log /var/log/nginx/stream_errors.log debug;
    # /etc/nginx/stream.d/ 디렉터리의 모든 .conf 파일 포함 
    server {
        listen 3003;
        proxy_pass hyukjin_store;
        ssl_preread on;
    }
    upstream hyukjin_store {
       server 10.10.44.5:80;
    }
}

예를 들어, curl localhost:3003을 실행하면 요청이 hyukjin_store 업스트림으로 전달되어 10.10.44.5:80으로 프록시된다.

 

 

Route53 에서 hyukjin.stroe 호스팅 영역에 A 레코드 생성

 

 

웹브라우저에 hyukjin.store:3003 으로 접속 확인

 

 

'리눅스' 카테고리의 다른 글

systemctl enable  (0) 2025.04.02
Redis Master/Slave + HAProxy  (0) 2025.03.18
Redis Failover  (0) 2025.03.13
Vmware 네트워크  (0) 2025.01.12
inode  (0) 2025.01.11