How to Fix Nginx 502 Bad Gateway: Solving “Resource Temporarily Unavailable” in PHP-FPM Connections


4 views

After analyzing your server configuration and error logs, we're dealing with a classic case of PHP-FPM connection exhaustion. The key error message:

connect() to unix:/var/lib/php7.0-fpm/web3.sock failed (11: Resource temporarily unavailable)

This occurs when Nginx can't establish new connections to your PHP-FPM backend because all available workers are busy or the connection queue is full.

From your status output, I notice several concerning patterns:

Status: "Processes active: 20, idle: 12, Requests: 6874, slow: 0, Traffic: 0.3req/sec"

Despite relatively low traffic (0.3 requests/sec), your PHP-FPM processes are consistently active. This suggests inefficient process management or resource starvation.

Here's how we should modify your PHP-FPM pool settings (/etc/php/7.0/fpm/pool.d/www.conf):

[www]
user = www-data
group = www-data
listen = /run/php/php7.0-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.backlog = 65536  # Increased from default 128
pm = dynamic
pm.max_children = 50    # Adjusted based on 12GB RAM
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500
pm.process_idle_timeout = 10s
request_terminate_timeout = 30s
request_slowlog_timeout = 5s

Adjust your Nginx configuration to better handle PHP-FPM connections:

http {
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 60;
    fastcgi_read_timeout 60;
    fastcgi_busy_buffers_size 64k;
    fastcgi_temp_file_write_size 256k;
}

For high-traffic servers, we need to adjust kernel parameters:

# Add to /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_tw_buckets = 1440000
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_window_scaling = 1

Apply changes with sysctl -p

Install these tools for ongoing monitoring:

sudo apt-get install htop sysstat
# Check PHP-FPM status
sudo systemctl status php7.0-fpm
# Monitor socket connections
ss -xlp | grep php-fpm

When issues occur, run these diagnostic commands:

# Check current connections
sudo netstat -anp | grep php-fpm | wc -l
# Check for queued connections
sudo ss -lntp | grep php-fpm
# Check process limits
cat /proc/$(pgrep php-fpm | head -1)/limits | grep 'open files'

Remember to restart services after configuration changes:

sudo systemctl restart php7.0-fpm
sudo systemctl restart nginx

When examining the error logs from /var/www/example/log/error.log, we see repeated occurrences of:

connect() to unix:/var/lib/php7.0-fpm/web3.sock failed (11: Resource temporarily unavailable)

This typically indicates that PHP-FPM has reached its process limit and cannot accept new connections, which in turn causes Nginx to return 502 Bad Gateway errors.

The current PHP-FPM pool configuration shows:

[www]    
pm = dynamic
pm.max_children = 12    
pm.start_servers = 2    
pm.min_spare_servers = 1    
pm.max_spare_servers = 3    
pm.max_requests = 500

Meanwhile, the systemctl status php7.0-fpm output reveals:

Status: "Processes active: 20, idle: 12, Requests: 6874, slow: 0, Traffic: 0.3req/sec"

The primary issue stems from resource exhaustion in PHP-FPM's process manager. Several factors contribute:

  • The pm.max_children setting (12) is too low for the server's capacity (6 cores, 12GB RAM)
  • Multiple PHP-FPM pools are competing for resources
  • The pm.max_requests setting causes frequent process recycling

For a server with 12GB RAM, consider these adjustments to /etc/php/7.0/fpm/pool.d/www.conf:

[www]
pm = dynamic
pm.max_children = 120
pm.start_servers = 12
pm.min_spare_servers = 6
pm.max_spare_servers = 18
pm.max_requests = 1000
pm.process_idle_timeout = 10s

Add these parameters to your Nginx fastcgi configuration:

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_connect_timeout 60s;
    fastcgi_read_timeout 60s;
    fastcgi_send_timeout 60s;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    include fastcgi_params;
}

Implement regular monitoring with these commands:

# Check PHP-FPM status
sudo systemctl status php7.0-fpm

# View active connections
sudo netstat -anp | grep php-fpm

# Monitor process count
watch -n 5 "ps aux | grep php-fpm | wc -l"

If issues persist:

  1. Increase kernel-level connection limits:
    sysctl -w net.core.somaxconn=65535
    sysctl -w net.ipv4.tcp_max_syn_backlog=65535
    
  2. Adjust file descriptor limits for both Nginx and PHP-FPM:
    # In /etc/security/limits.conf
    www-data soft nofile 65535
    www-data hard nofile 65535