Optimizing Nginx keepalive_timeout for High-Traffic PHP-FPM and MySQL Sites


2 views

The keepalive_timeout directive in Nginx controls how long a keep-alive connection stays open between the client and server. For a busy PHP-FPM site with MySQL backend, this setting significantly impacts both performance and resource utilization.

While the default keepalive_timeout is typically 75 seconds, many production environments require fine-tuning:


# Default setting
keepalive_timeout 75s;

# Common production setting
keepalive_timeout 15s;

When adjusting this value for your MySQL-backed PHP application:

  • Higher values reduce TCP handshake overhead but consume more server resources
  • Lower values free up resources but may increase latency for subsequent requests
  • The sweet spot depends on your traffic patterns and server capacity

For a busy site with PHP-FPM processing, consider this configuration:


http {
    keepalive_timeout 10s;
    keepalive_requests 100;
    
    server {
        location ~ \.php$ {
            fastcgi_keep_conn on;
            # Other PHP-FPM settings...
        }
    }
}

Use these commands to monitor connection status:


# View active connections
netstat -anp | grep nginx | grep ESTABLISHED | wc -l

# Check keepalive statistics
nginx -T | grep keepalive

For extremely busy sites, combine with these settings:


events {
    worker_connections 4096;
    multi_accept on;
}

http {
    keepalive_timeout 5s;
    keepalive_requests 500;
    reset_timedout_connection on;
}

Use Apache Benchmark to test different timeout values:


ab -k -n 1000 -c 100 http://yoursite.com/test.php

Monitor server metrics during tests to find the optimal balance between performance and resource usage.


The keepalive_timeout directive in Nginx controls how long a connection should stay open after processing a request, allowing subsequent requests to reuse the same connection. This is particularly important for busy websites where connection overhead can impact performance.

# Default Nginx keepalive_timeout (when not specified)
keepalive_timeout 75s;

When using PHP-FPM with Nginx, we need to balance between:

  • Connection reuse efficiency
  • Server resource consumption
  • Client behavior patterns
  • PHP script execution times

For a MySQL-based PHP application, consider these factors:

# Sample optimized configuration
keepalive_timeout 15s;
keepalive_requests 100;

Rationale for these values:

  • 15 seconds timeout: Gives enough time for subsequent AJAX calls or page assets while not holding connections too long
  • 100 requests per connection: Allows good connection reuse without excessive memory usage

Use these commands to monitor connection usage:

# View active connections
ss -s | grep -i "tcp"

# Monitor connection states
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

For high-traffic scenarios, consider these additional directives:

http {
    keepalive_timeout 15s;
    keepalive_requests 100;
    
    # Timeout for reading client request headers
    client_header_timeout 10s;
    
    # Timeout for reading client request body
    client_body_timeout 10s;
    
    # Timeout for transmitting response to client
    send_timeout 10s;
    
    # Enable keepalive connections
    keepalive_disable none;
}

Watch for these symptoms that might indicate keepalive problems:

  • High TIME_WAIT connections (check with netstat -nat | grep TIME_WAIT | wc -l)
  • Increasing memory usage under load
  • Slow response times despite low CPU usage