Nginx PHP-FPM stops working, and an open socket is left in the connection

Solution:

About the first timeout error you see, if you are using a FastCGI configuration, then you might need to review this:

fastcgi_read_timeout 600s;

Make sure that value is longer than the longest processing PHP will be doing.

You should also definitely increase worker connections setting to whatever expected number of simultaneous connections you expect (say 10000):

worker_connections 10000;

Also, as mentioned by Sang Lu's answer, you need to make sure nginx can open enough file handles (which includes network sockets as well).

If you start the master nginx process as root, you can simply do (at least two times of the worker_connections configuration above:

worker_rlimit_nofile 21000;

The other way is to use ulimit or /etc/security/limits.conf for the user that starts the master nginx process.

Answer

Solution:

Probably it is that your Nginx process is opening too many files. You can increase this limit by following this article: How to Increase Number of Open Files Limit in Linux

To change this setting, you need the root privilege and do as follows:

  1. Open the /etc/security/limits.conf file: vi /etc/security/limits.conf

  2. Append those lines at the end of the file:

    ## Example hard limit for maximum number of opened files
    *    hard    nofile    10240
    ## Example soft limit for maximum number of opened files
    *    soft    nofile    10240
    
  3. Save all changes and Reboot your Server/VPS to apply new settings.

Note: The number 10240 is the maximum number of files that a process can open. Change this value as your needs.

Answer

Solution:

Here are a few steps you can take to diagnose and resolve the issue:

  1. Increasing PHP-FPM timeouts requires changing the request_terminate_timeout, request_slowlog_timeout, and max_execution_timesettings in your PHP-FPM configuration file (often located at/etc/php-fpm.confor/etc/php/version/fpm/php-fpm.conf)`. Once the changes have been made, restart PHP-FPM.

  2. Increase Nginx timeouts: In your Nginx configuration file (usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf), adjust the proxy_read_timeout and fastcgi_read_timeout values to allow for longer request processing times. Restart Nginx after making the changes.

  3. Verify the server's resources: Keep an eye on the server's resource utilization during periods of high traffic. Check to see if your CPU, memory, and disk I/O are not running out of space. If required, think about updating your server or streamlining the code and database queries in your application to boost performance.

  4. Enable the PHP-FPM slow log to find any sluggish requests that can be the reason for the timeouts. Uncomment the'slowlog' directive and set its value to the directory where you wish to store the log file in your PHP-FPM configuration file. Once the changes have been made, restart PHP-FPM. Look for any bottlenecks in your application code by analyzing the log file.

  5. Implement health checks: To run routine health checks on your Nginx and PHP-FPM servers, you can set up a monitoring system or use third-party tools. These checks can assist in finding any problems before they impair service. The server's response time, CPU use, memory utilization, and other pertinent data can all be observed.

  6. Enable error logging: Ensure that PHP-FPM and Nginx error logging are both turned on. Look through the error logs for any pertinent error messages that can offer more information about the reason for the timeouts.

Source