Optimizing Nginx Reverse Proxy Performance for High-Load Environments
When operating high-throughput network endpoints, standard Linux and Nginx configurations often become a bottleneck. Under severe load, servers hit boundary conditions such as ephemeral port exhaustion, file descriptor allocation limits, and sub-optimal TCP window clamping. This guide outlines practical parameters to enforce robust L4/L7 traffic proxying.
1. Linux Kernel Tuning (sysctl.conf)
Before optimizing Nginx itself, the host underlying network stack must be configured to handle large connection queues. Append the following settings to your /etc/sysctl.conf:
# Maximize local port ranges allocation
net.ipv4.ip_local_port_range = 1024 65535
# Enable TCP window scaling and fast recycle
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_reuse = 1
# Maximize backlog queues
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.core.netdev_max_backlog = 65536
2. Optimizing nginx.conf Core Directives
Ensure that file descriptor allocation (worker_rlimit_nofile) scales linearly with the connection limit. A worker handling multiple connections must never drop frames due to filesystem pooling constraints.
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 8192;
use epoll;
multi_accept on;
}
3. HTTP/2 and Buffer Management
Mitigate L7 response clipping by increasing client body buffers. This prevents Nginx from creating temporary files on disk, moving the overhead strictly to volatile memory pools.
http {
include mime.types;
default_type application/octet-stream;
# Timeouts
keepalive_timeout 65;
keepalive_requests 10000;
send_timeout 10;
# Buffer Configurations
client_body_buffer_size 128k;
client_max_body_size 10m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
# Fast TCP Data Transfer
sendfile on;
tcp_nopush on;
tcp_nodelay on;
}