sysctl and TCP settings for a busy WordPress server: what actually matters
Lists of “kernel tuning for high traffic” circulate with dozens of sysctl values, most of them copied from servers with very different workloads. On a WordPress server, the limits that are reached in practice are few: the connection backlog when a traffic spike arrives, the number of open files, and the TIME_WAIT sockets between Nginx and PHP-FPM or upstream services. Everything else should stay at the distribution’s defaults until a measurement says otherwise.
Problem: During a campaign, a WordPress server returns occasional “502 Bad Gateway” and connection timeouts, while CPU and memory still have headroom. The Nginx error log shows connect() to unix:/run/php/php8.3-fpm.sock failed (11: Resource temporarily unavailable).
Solution: Raise net.core.somaxconn and the PHP-FPM listen.backlog together, raise the open file limits for Nginx, check the TCP counters before and after, and keep a short, commented sysctl file with only the values that were measured to matter.
Measure first
ss -s # sockets by state ss -lx | grep php # Unix socket of PHP-FPM: Recv-Q = waiting connections netstat -s | grep -iE 'listen|overflow|SYNs to LISTEN' cat /proc/sys/net/core/somaxconn
“times the listen queue of a socket overflowed” growing during load means connections were dropped because the backlog was full. For a Unix socket, Recv-Q in ss -lx shows how many connections wait for a PHP-FPM worker; when it reaches the backlog, Nginx gets error 11 and returns 502.
The backlog: kernel and PHP-FPM together
# /etc/sysctl.d/90-wordpress.conf # Max. pending connections per listening socket (default 4096 on kernel 5.4+, 128 on older kernels) net.core.somaxconn = 4096 # SYN backlog for TCP listeners (Nginx on :443) net.ipv4.tcp_max_syn_backlog = 4096
; /etc/php/8.3/fpm/pool.d/www.conf listen.backlog = 1024
sudo sysctl --system sudo systemctl reload php8.3-fpm
The effective backlog of a socket is the smaller of the application’s value and somaxconn. Raising only one of them changes nothing. A longer queue does not create capacity: it lets short spikes wait a few hundred milliseconds instead of failing. If the queue is full for minutes, the server needs more PHP-FPM workers or less work per request: tune the PHP-FPM pool.
Open files
Every connection is a file descriptor. Nginx needs two per proxied request (client and upstream):
# /etc/nginx/nginx.conf
worker_rlimit_nofile 65535;
events {
worker_connections 8192;
}
worker_rlimit_nofile raises the limit for the worker processes without touching the system-wide settings; the systemd unit’s LimitNOFILE applies to the master. “Too many open files” in the Nginx error log is the signal that these are too low.
TIME_WAIT and ephemeral ports
When Nginx talks to a backend over TCP (PHP-FPM on 127.0.0.1:9000, a Redis or an API), each closed connection stays in TIME_WAIT for 60 seconds and blocks its local port. At thousands of requests per second, the default port range can run out:
net.ipv4.ip_local_port_range = 10240 65535 net.ipv4.tcp_tw_reuse = 1
tcp_tw_reuse allows reusing TIME_WAIT sockets for new outgoing connections and is safe. The often recommended tcp_tw_recycle was removed from Linux in kernel 4.12 because it broke clients behind NAT. A simpler fix for PHP-FPM is a Unix socket instead of TCP, which has no ports at all.
What to leave alone
Buffer sizes (rmem/wmem), congestion control and vm.* memory settings matter for servers that push large files over long-distance links or run databases with special needs. For a WordPress server, the defaults of a current kernel are good; changes without measurement often make things worse. Watch the effect of each change with the same counters as before, under comparable load: htop, vmstat and iostat.
NOTE: Most “high traffic” problems of WordPress are solved one layer higher: a full-page cache answers anonymous visitors without PHP, which removes the backlog problem for the majority of requests: Nginx FastCGI cache.