← 블로그 목록
작성일: 2026-05-31 Nginx성능최적화

Nginx 로그 분석 기반 성능 최적화 가이드

로그 분석으로 병목 지점을 찾아내고 Nginx 설정을 최적화하는 실전 가이드입니다. 응답시간, 캐시 히트율, 압축 효율을 로그 데이터로 측정하고 개선합니다.


응답시간 로깅 추가

기본 로그 포맷에 $request_time$upstream_response_time 추가:

``nginx log_format optimized '$remote_addr - [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" rt=$request_time uct=$upstream_connect_time uht=$upstream_header_time urt=$upstream_response_time';

access_log /var/log/nginx/access.log optimized; `

필드 의미:

  • $request_time: 총 요청 처리 시간
  • $upstream_connect_time: 백엔드 연결 시간
  • $upstream_header_time: 백엔드 헤더 응답 시간
  • $upstream_response_time: 백엔드 전체 응답 시간

느린 요청 분석

P95 응답시간 계산

`bash awk '{print $(NF-6)}' /var/log/nginx/access.log | sed 's/rt=//' | sort -n > times.txt P95=$(sed -n "$(($(wc -l < times.txt) * 95 / 100))p" times.txt) echo "P95: ${P95}s" `

1초 이상 걸린 요청

`bash awk '$NF ~ /urt=[1-9]/ || $NF ~ /urt=[0-9]{2,}/' /var/log/nginx/access.log `

가장 느린 경로 Top 10

`bash awk '{print $(NF-6), $7}' /var/log/nginx/access.log | sed 's/rt=//' | sort -rn | head -10 `


정적 파일 캐싱 효율 측정

캐시 상태 로깅

`nginx log_format cache '$remote_addr - "$request" $status $upstream_cache_status'; access_log /var/log/nginx/cache.log cache; `

$upstream_cache_status 값:

  • HIT: 캐시에서 제공
  • MISS: 캐시 없음, 백엔드 요청
  • BYPASS: 캐시 우회
  • EXPIRED: 캐시 만료
  • -: 캐시 사용 안 함

캐시 히트율 계산

`bash TOTAL=$(wc -l < /var/log/nginx/cache.log) HIT=$(grep 'HIT' /var/log/nginx/cache.log | wc -l) echo "Hit Rate: $((100 * HIT / TOTAL))%" `

80% 이상이 이상적입니다.


Gzip 압축 효율 확인

압축 전후 비교

`nginx log_format gzip '$remote_addr - "$request" $status $body_bytes_sent $gzip_ratio'; access_log /var/log/nginx/gzip.log gzip; `

$gzip_ratio: 압축률 (예: 3.5 = 3.5배 압축)

평균 압축률

`bash awk '{print $NF}' /var/log/nginx/gzip.log | awk '{sum+=$1; n++} END {print sum/n}' `


연결 제한 최적화

동시 연결 수 모니터링

`bash netstat -an | grep :80 | wc -l `

worker_connections 설정

/etc/nginx/nginx.conf:

`nginx events { worker_connections 4096; # 기본값 1024 → 4096 use epoll; # Linux에서 효율적 } `

최대 연결 수 = worker_processes × worker_connections


Keep-Alive 최적화

Keep-Alive 설정

`nginx http { keepalive_timeout 65; keepalive_requests 100; } `

로그로 효과 확인

Keep-Alive 사용 시 동일 IP의 연속 요청 간격이 짧습니다:

`bash awk '{print $1, $4}' /var/log/nginx/access.log | sort | uniq -c `


백엔드 연결 풀

Upstream Keep-Alive

`nginx upstream backend { server 127.0.0.1:9000; keepalive 32; }

server { location ~ \.php$ { fastcgi_pass backend; fastcgi_keep_conn on; } } `

로그로 연결 재사용 확인

$upstream_connect_time = 0.000 → 재사용됨

`bash awk '$NF ~ /uct=0\.000/' /var/log/nginx/access.log | wc -l `


정적 파일 최적화

Expires 헤더

`nginx location ~* \.(jpg|jpeg|png|gif|css|js)$ { expires 30d; add_header Cache-Control "public, immutable"; } `

로그에서 304 응답 확인

`bash grep ' 304 ' /var/log/nginx/access.log | wc -l `

304 (Not Modified) 비율이 높을수록 캐싱이 잘 됩니다.


대용량 파일 전송 최적화

sendfile 활성화

`nginx http { sendfile on; tcp_nopush on; tcp_nodelay on; } `


정리

로그에 $request_time, $upstream_response_time, $upstream_cache_status`를 추가하면 응답시간, 캐시 히트율, 백엔드 병목을 정량적으로 측정할 수 있습니다. 이 데이터로 worker_connections, keepalive, 캐싱 정책을 최적화하세요.