기본 설치 상태의 Nginx는 보수적인 설정값을 가지고 있어 서버 자원을 충분히 활용하지 못합니다. 올바른 튜닝으로 처리량을 수 배 끌어올릴 수 있습니다.

현재 상태 확인

# Nginx 버전 및 컴파일 옵션 확인
nginx -V

# 현재 연결 수 확인
ss -s

# CPU 코어 수 확인
nproc

worker_processes 설정

/etc/nginx/nginx.conf에서 핵심 설정을 조정합니다:

# CPU 코어 수에 맞게 자동 설정 (권장)
worker_processes auto;

events {
    # 워커 당 최대 동시 연결 수
    # 울트라한 서버: 1024~4096, 일반: 512~1024
    worker_connections 1024;

    # Linux epoll 이벤트 모델 사용 (성능 향상)
    use epoll;

    # 연결 요청을 한 번에 수락
    multi_accept on;
}

HTTP 블록 최적화

http {
    # sendfile 시스템 콜로 파일 전송 (커널 공간에서 직접 처리)
    sendfile on;

    # sendfile과 함께 사용, 패킷 전송 최적화
    tcp_nopush on;

    # 작은 패킷 즉시 전송 (API 서버에 유용)
    tcp_nodelay on;

    # Keep-Alive 타임아웃 (초)
    keepalive_timeout 65;

    # Keep-Alive 최대 요청 수
    keepalive_requests 100;

    # 해시 테이블 최적화
    types_hash_max_size 2048;
    server_names_hash_bucket_size 64;
}

버퍼 크기 설정

http {
    # 클라이언트 요청 헤더 버퍼
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;

    # 클라이언트 요청 바디 버퍼 (POST 데이터)
    client_body_buffer_size 128k;
    client_max_body_size 10m;   # 업로드 최대 크기

    # 프록시 버퍼 (리버스 프록시 사용 시)
    proxy_buffering on;
    proxy_buffer_size 4k;
    proxy_buffers 8 4k;
    proxy_busy_buffers_size 8k;
}

gzip 압축 설정

텍스트 기반 응답(HTML, CSS, JS, JSON)을 압축하면 전송량을 60~80% 줄일 수 있습니다:

http {
    gzip on;
    gzip_vary on;
    gzip_proxied any;

    # 압축 레벨 (1~9, 높을수록 CPU 소모 증가)
    # 6이 속도와 압축률의 균형점
    gzip_comp_level 6;

    # 이 크기 이상의 응답만 압축 (작은 파일은 압축 효율이 낮음)
    gzip_min_length 1000;

    # 압축할 MIME 타입
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/rss+xml
        image/svg+xml;
}

정적 파일 캐싱 헤더

server {
    # 정적 파일에 캐시 헤더 추가
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }
}

타임아웃 설정

http {
    # 클라이언트 요청 타임아웃
    client_header_timeout 10s;
    client_body_timeout 10s;
    send_timeout 10s;

    # 느린 연결 차단 (Slowloris 방어 효과)
    reset_timedout_connection on;
}

설정 적용 및 검증

# 설정 파일 문법 검사
nginx -t

# 무중단 재로드
sudo systemctl reload nginx

# 응답 속도 측정 (ab 벤치마크)
ab -n 1000 -c 100 https://your-domain.com/

# gzip 압축 확인
curl -H "Accept-Encoding: gzip" -I https://your-domain.com/

성능 모니터링

# /etc/nginx/conf.d/status.conf
server {
    listen 127.0.0.1:80;
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}
curl http://127.0.0.1/nginx_status
# Active connections: 145
# server accepts handled requests
#  1234567 1234567 2345678
# Reading: 0 Writing: 12 Waiting: 133

올바른 튜닝만으로도 동일 하드웨어에서 처리할 수 있는 요청 수가 크게 증가합니다. TCP-80.NET의 서버는 기본적으로 최적화된 Nginx 설정을 제공하며, 추가 튜닝이 필요하시면 텔레그램 @tcp80net으로 문의하세요.