Let's take PHP-FPM as an example to analyze the problem. As we mentioned at the beginning, due to a backend server failure, the number of PHP-CGI processes is insufficient, the PHP Execution time is long, or the PHP-CGI process is dead, and FastCGI Cache Usage on the nginx end (if you are using a proxy, pay attention to the usage of the proxy cache), then we should start with these questions to learn about the problem. First, you need to determine whether the backend server is started. Of course, the PHP-FPM process is not started here. If the process is not started, or the process exits due to an error for some reason, the access result is "502 Bad Gateway: [Root @ mail php5.3.4] # ps-Ef | grep PHP-FPM Root 15375 1 0 :46? 00:00:00 PHP-FPM: Master process (/usr/local/php5.3.4/etc/php-fpm.conf) Nobody 15376 15375 0? 00:00:00 PHP-FPM: pool WWW ...... // Omitted Nobody 15380 15375 0? 00:00:00 PHP-FPM: pool WWW Nobody 15381 15375 0? 00:00:00 PHP-FPM: pool W3 Nobody 15382 15375 0? 00:00:00 PHP-FPM: pool W3 ...... // Omitted Nobody 15385 15375 0? 00:00:00 PHP-FPM: pool W3
The second step is to determine whether the PHP-FPM worker process is sufficient. If it is not enough, it is like the background service is not enabled for access: Calculate the number of worker processes enabled:
[Root @ mail php5.3.4] # ps-Ef | grep PHP-FPM | WC-l 122
Then subtract 2 (one is the master process master, and the other is the grep here) from the Worker Process of the PHP-FPM you enabled. Of course, if you are not in too much trouble, you can use the following command: [Root @ mail php5.3.4] # ps-Ef | grep PHP-FPM | grep-V "master" | grep-V "grep" | WC-l 120 Calculate the used Worker Process (the process that is processing the request ): [Root @ mail php5.3.4] # netstat-anp | grep PHP-FPM | WC-l 124
And then subtract at least 2 or more (one is listening to listen, two is the php-fpm.conf), the number of listen depends on the number of processes using the pool, for example, here we open two process pools WWW and W3. If you are not afraid of trouble, it is better to execute the following command: [Root @ mail php5.3.4] # netstat-anp | grep "PHP-FPM" | grep-V "listen" | grep-V "php-fpm.conf" | WC-l 120
If the two values are similar, you can increase the number of worker processes. Third, FastCGI buffer (buffer) or proxy buffer. In the FastCGI module, there are two buffer-related commands:
Fastcgi_buffer_size 4 K Fastcgi_buffers 16 4 K
The setting of the First Command depends on the memory page settings of the operating system. This can be found from the operating system: [Root @ S8 ~] # Getconf pagesize 4096
The unit of this value is byte, that is, 4 K. The parameters of the second command specify the buffer size to be used to read the response header from the FastCGI process. The value is determined by the result of the two Commands: 16*4 k = 64 K. Therefore, this means that for all the responses returned by FastCGI, nginx writes more than 64 KB of data to the disk, and some less than 64 KB of data to the memory. If the waiting time you set is too short and the machine is busy, it will inevitably cause 502 problems. If you want a proxy module, the following settings will apply: Proxy_buffer_size 16 K; Proxy_buffers 4 16 K; The interpretation method is equivalent to the FastCGI buffer, so I will not talk about it any more. Fourth, PHP Execution takes a long time. In the FastCGI module, there are three commands related to time settings. For the inconvenience caused by this problem, here is an extreme example. If you set the value of fastcgi_send_timeout to 1 second, it takes 5 seconds to complete the operation. In this case, it must be 502. Therefore, you need to adjust the values of the following three commands according to the actual situation:
Fastcgi_connect_timeout 60; Fastcgi_send_timeout 60; Fastcgi_read_timeout 60; Different website applications have different requirements. Therefore, you can modify the three configuration commands according to the application. If the proxy module is used, the related settings are as follows: Proxy_connect_timeout 60; Proxy_send_timeout 60; Proxy_read_timeout 60; Fail_timeout 30; The above commands are similar to the FastCGI module, so we will not talk about them any more. The last one is the instructions of the upstream module. If Server Load balancer is used, refer to the instructions. Finally, as a learning and reference method, you should always check the PHP-FPM logs, especially when there is a problem, you should adjust the log level to the debug level, to understand the source of the problem. |