The error message connect() failed (111: Connection refused) while connecting to upstream
typically occurs when Nginx cannot establish a connection to the PHP-FPM service. This results in a 502 Bad Gateway
error, indicating the web server cannot communicate with the upstream (PHP processor).
Several factors could trigger this issue:
- PHP-FPM service not running
- Incorrect socket or port configuration
- Firewall blocking the connection
- Permission issues
- Resource exhaustion
First, verify if PHP-FPM is running:
sudo systemctl status php-fpm
If it's not running, start it with:
sudo systemctl start php-fpm
Ensure your Nginx server block has the correct FastCGI configuration. Here's a proper example:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# or use TCP connection:
# fastcgi_pass 127.0.0.1:9000;
}
You can configure PHP-FPM to use either Unix sockets or TCP ports. Check your /etc/php/7.4/fpm/pool.d/www.conf
(adjust version as needed):
; For Unix socket:
listen = /var/run/php/php7.4-fpm.sock
; For TCP connection:
; listen = 127.0.0.1:9000
If using Unix sockets, ensure proper permissions:
sudo chown www-data:www-data /var/run/php/php7.4-fpm.sock
sudo chmod 660 /var/run/php/php7.4-fpm.sock
For TCP connections, verify the port isn't blocked:
sudo ufw allow 9000/tcp
sudo netstat -tulnp | grep 9000
If the issue persists, check PHP-FPM logs:
sudo tail -f /var/log/php7.4-fpm.log
And increase PHP-FPM's pm.max_children if you're hitting resource limits:
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
Here's a complete Nginx server block configuration that works with PHP-FPM:
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
The error message connect() failed (111: Connection refused) while connecting to upstream
typically occurs when Nginx cannot establish a connection to the PHP-FPM service. This results in a 502 Bad Gateway
error being returned to the client.
Several factors could lead to this issue:
- PHP-FPM service is not running
- Incorrect socket or port configuration
- Firewall blocking the connection
- Permission issues on the socket file
- Resource exhaustion (too many connections)
First, check if PHP-FPM is running:
sudo systemctl status php-fpm
# or
sudo service php-fpm status
If it's not running, start it:
sudo systemctl start php-fpm
# or
sudo service php-fpm start
Verify your Nginx and PHP-FPM configuration. Here's a typical setup:
Nginx configuration (usually in /etc/nginx/sites-available/your-site
):
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# or for TCP connection:
# fastcgi_pass 127.0.0.1:9000;
}
PHP-FPM pool configuration (usually in /etc/php/7.4/fpm/pool.d/www.conf
):
[www]
listen = /var/run/php/php7.4-fpm.sock
; or for TCP connection:
; listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
If using Unix sockets (recommended for better performance):
- Ensure the socket file exists
- Verify permissions (www-data should have access)
- Check the path matches in both Nginx and PHP-FPM config
If using TCP connection:
- Verify PHP-FPM is listening on the correct port
- Check firewall rules (e.g., ufw or iptables)
- Test connectivity with
telnet 127.0.0.1 9000
If the issue persists, try these steps:
# Check for port conflicts
sudo netstat -tulnp | grep 9000
# Check socket file permissions
ls -la /var/run/php/
# Increase PHP-FPM connection limits
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
After making changes:
sudo systemctl restart php-fpm
sudo systemctl restart nginx
sudo tail -f /var/log/nginx/error.log
Remember to check both Nginx and PHP-FPM error logs for more detailed information about the issue.