GrepBoard/매뉴얼
605 편 ·

nginx worker_connections keepalive 튜닝

·시험 환경 nginx 1.30.6 (Docker nginx:stable) · 2026-10·시험 범위 ↓·6 단계

nginx 동시 연결은 작업자 수 × worker_connections 이고 열린 파일 한도를 못 넘습니다. worker_connections are not enough 로그와 파일 한도를 보고 올립니다.

nginx 가 동시에 받을 수 있는 연결은 대략 worker_processes × worker_connections 이고, 리버스 프록시라면 요청 하나가 클라이언트 쪽과 업스트림 쪽 연결 두 개를 씁니다. 그리고 실제 한도는 작업자 프로세스의 열린 파일 수(RLIMIT_NOFILE)를 넘지 못합니다. 먼저 오류 로그의 worker_connections are not enough 와 작업자의 Max open files 를 보고, 둘 다 맞춰 올립니다.

[1] 지금 값을 봅니다. nginx -T 는 포함된 파일까지 합친 실제 설정을 보여 줍니다.

console
$ nginx -T 2>/dev/null | grep -nE 'worker_processes|worker_connections|sendfile|tcp_nopush|keepalive_timeout|worker_rlimit_nofile'4:worker_processes  auto;11:    worker_connections  1024;25:    sendfile        on;26:    #tcp_nopush     on;28:    keepalive_timeout  65;

이것은 공식 Docker 이미지의 nginx.conf 입니다. 지시어를 아예 쓰지 않으면 모듈 기본값이 적용됩니다.

지시어모듈 기본공식 이미지 nginx.conf
worker_processes1auto(CPU 수)
worker_connections5121024
worker_rlimit_nofile없음(물려받은 한도)없음
keepalive_timeout75s65
sendfileoffon

작업자가 실제로 받은 열린 파일 한도는 /proc/<작업자 PID>/limits 에 있습니다. 시험 컨테이너는 4코어라 작업자가 4개였고, 소프트 한도가 1024 였습니다.

console
$ for p in /proc/[0-9]*; do tr "\0" " " < $p/cmdline 2>/dev/null | grep -q "^nginx: worker" && grep "Max open files" $p/limits; doneMax open files            1024                 524288               files     Max open files            1024                 524288               files     Max open files            1024                 524288               files     Max open files            1024                 524288               files     

일반 서버에서는 pgrep -f 'nginx: worker' 로 PID 를 찾아 같은 파일을 봅니다.

[2] 오류 로그로 모자란지 봅니다. 경고가 두 단계로 나옵니다. 시험은 worker_processes 1 · worker_connections 8 로 띄운 서버에 연결 10개를 붙여 냈습니다.

output
[warn] 29#29: 8 worker_connections are not enough, reusing connections
output
[alert] 29#29: 8 worker_connections are not enough
로그뜻
[warn] … not enough, reusing connections쉬고 있는 연결(keepalive·요청 대기)을 닫아 자리를 만들었다 — 새 요청은 받았다(시험에서 200)
[alert] … not enough회수할 연결이 없어 새 연결을 버렸다 — 클라이언트는 연결이 끊긴다(시험에서 curl 종료 코드 56)

warn 이 자주 보이면 keepalive 연결이 자리를 먹고 있다는 뜻이고, alert 가 보이면 실제로 연결을 잃고 있습니다.

[3] worker_connections 를 올립니다. events 블록에 있고, 설정을 다시 읽으면(reload) 새 작업자부터 적용됩니다. 고치기 전에 지금 파일을 남겨 둡니다.

console
$ sudo cp -a /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

아래는 파일 전체가 아니라 바꿀 지시어만 보인 조각입니다. 기존 파일에서 이 지시어만 더하거나 고치고, 파일을 이 내용으로 덮어쓰지 않습니다(… 는 그대로 두는 나머지 줄 · http { … } 등).

/etc/nginx/nginx.conf
…worker_processes auto;worker_rlimit_nofile 16384;…events {    worker_connections 4096;}…
console
$ sudo nginx -t$ sudo nginx -s reload

값의 근거는 「작업자당 최대 동시 연결 × 프록시면 2」입니다. 공식 문서대로 이 수에는 업스트림 연결도 들어갑니다. 되돌리려면 백업 파일을 다시 복사한 뒤 nginx -t 와 reload 를 합니다.

[4] 열린 파일 한도를 함께 맞춥니다. worker_connections 만 올리면 nginx -t 가 경고합니다.

console
$ docker run --rm --ulimit nofile=1024:1024 -v ./big.conf:/etc/nginx/nginx.conf:ro nginx:stable nginx -t…nginx: the configuration file /etc/nginx/nginx.conf syntax is ok2026/10/27 11:20:43 [warn] 1#1: 4096 worker_connections exceed open file resource limit: 1024nginx: [warn] 4096 worker_connections exceed open file resource limit: 1024nginx: configuration file /etc/nginx/nginx.conf test is successful

worker_rlimit_nofile 을 넣으면 작업자가 스스로 한도를 올리고 경고가 사라집니다. 하지만 하드 한도보다 높게는 못 올립니다. 하드 한도가 1024 인 컨테이너에서는 nginx -t 가 통과했는데도 실제 작업자는 이렇게 남았습니다.

output
[alert] 28#28: setrlimit(RLIMIT_NOFILE, 8192) failed (1: Operation not permitted)
console
$ for p in /proc/[0-9]*; do tr "\0" " " < $p/cmdline 2>/dev/null | grep -q "^nginx: worker" && grep "Max open files" $p/limits; doneMax open files            1024                 1024                 files     

하드 한도를 65536 으로 준 컨테이너에서는 같은 설정으로 8192 가 됐습니다.

console
$ for p in /proc/[0-9]*; do tr "\0" " " < $p/cmdline 2>/dev/null | grep -q "^nginx: worker" && grep "Max open files" $p/limits; doneMax open files            8192                 8192                 files     

그러니 nginx -t 통과만 보지 말고 시작 로그의 setrlimit 줄과 작업자의 limits 를 확인합니다. 하드 한도는 nginx 를 띄우는 쪽이 정합니다 — systemd 서비스면 LimitNOFILE=(systemctl edit nginx 로 덮어쓰기), 컨테이너면 --ulimit nofile=<소프트>:<하드> 입니다.

[5] keepalive 를 봅니다. 클라이언트 쪽 keepalive_timeout 은 쉬는 연결을 얼마나 붙잡을지입니다. 연결 자리가 모자라고 reusing connections 경고가 많으면 이 값을 줄이는 것이 먼저입니다.

업스트림 쪽은 판에 따라 다릅니다. 1.29.7 부터는 업스트림 keepalive 가 기본으로 켜져(작업자당 쉬는 연결 32개 · local) proxy_http_version 기본도 1.1 입니다. 1.30.6 에서 upstream 블록에 server 만 적고 요청 세 번을 보내자, 백엔드가 본 클라이언트 포트가 셋 다 같았습니다(연결 재사용).

output
8081 35632 GET /a HTTP/1.18081 35632 GET /b HTTP/1.18081 35632 GET /c HTTP/1.1

keepalive 0; 을 넣으면 요청마다 새 연결이었습니다.

output
8081 35644 GET /a HTTP/1.18081 35650 GET /b HTTP/1.18081 35656 GET /c HTTP/1.1

1.29.7 보다 오래된 판(예 Ubuntu 24.04 패키지 1.24)에서는 직접 켭니다.

/etc/nginx/conf.d/app.conf
upstream app {    server 127.0.0.1:8080;    keepalive 16;}server {    location / {        proxy_pass http://app;        proxy_http_version 1.1;        proxy_set_header Connection "";    }}

keepalive 숫자는 전체 연결 수 한도가 아니라 작업자마다 남겨 두는 쉬는 연결 수입니다. 공식 문서는 업스트림이 새 연결도 받을 수 있도록 작게 잡으라고 합니다.

[6] 정적 파일을 많이 내보내면 sendfile on 과 함께 tcp_nopush on 을 켭니다(tcp_nopush 는 sendfile 을 쓸 때만 동작합니다). 같은 파일을 반복해 열면 open_file_cache max=10000 inactive=30s; 처럼 파일 기술자 캐시를 둡니다 — 캐시한 기술자도 열린 파일 한도를 씁니다. 리스닝 소켓의 대기열(listen 80 backlog=…, 리눅스 기본 511)은 커널 somaxconn 과 함께 정합니다(리눅스 somaxconn · backlog 튜닝). Apache 에서 같은 증상은 Apache MaxRequestWorkers reached를 봅니다.

공식 근거: Core functionality — worker_connections · worker_processes · worker_rlimit_nofile · ngx_http_upstream_module — keepalive(1.29.7 기본 변경) · ngx_http_core_module — keepalive_timeout · sendfile · tcp_nopush · open_file_cache · listen backlog

Nginx 로그를 브라우저에서 바로 분석 ↗ — 아무것도 올라가지 않습니다.

#nginx#worker_connections#worker_rlimit_nofile#keepalive#ulimit