Resolving "connect() failed (111: Connection refused) while connecting to upstream" in Nginx Configuration
The error message "connect() failed (111: Connection refused) while connecting to upstream" often indicates an issue in a server configuration or a service dependancy. For nginx, such errors typicaly involve commmunication with an upstream service, such as PHP-FPM for PHP processing.
When diagnosing this error, consider these primary causes:
- PHP-FPM Service Not Active:
Verify whether the PHP-FPM service is running. Use the following command to check if the process is listening on the expected port (9000 in the default configuraton):
If the service is inactive, start it by executing:netstat -tuln | grep :9000/usr/local/php/sbin/php-fpm - Exceeding PHP-FPM Process Limits:
PHP-FPM configurations limit the number of concurrent children processes that can handle requests. If this threshold is exceeded, incoming connections might be refused. To resolve:
- Open the PHP-FPM configuration file
php-fpm.confand locate the directive:pm.max_children - Increase its value appropriately based on traffic and system resource availability.
- Save the changes and restart PHP-FPM to apply:
service php-fpm restart
- Open the PHP-FPM configuration file
By identifying whether the service is inactive or constrained by resource limits, you can address the "Connection refused" error and restore normal functionality.