Home > Backend Development > PHP Tutorial > The ultimate solution to Nginx prompts 502 and 504 errors

The ultimate solution to Nginx prompts 502 and 504 errors

WBOY
Release: 2016-08-08 09:32:43
Original
1541 people have browsed it

Large memory can increase the following parameters, which can effectively reduce 502 errors


php-fpmThe main parameters to modify
128 / /Each one consumes about 20M memory, set according to the memory size
1024 //If each max_children process exceeds this number, it will be automatically killed and will be automatically rebuilt when used in the future. . Generally set to around 1000.
0s //If your server performance is good enough, and the broadband resources are sufficient, and there are no loops or bugs in the PHP script, you can directly set "request_terminate_timeout" to 0s . The meaning of 0s is to let PHP-CGI continue to execute without time limit. And if you can't do this, that is to say, your PHP-CGI may have a BUG, ​​or your bandwidth is not sufficient, or other reasons cause your PHP-CGI to be suspended, then it is recommended that you assign "request_terminate_timeout" A value that can be set based on the performance of your server. Generally speaking, the better the performance, the higher you can set it, anywhere from 20 minutes to 30 minutes. Since my server PHP scripts need to run for a long time, some may take more than 10 minutes, so I set 900 seconds so that PHP-CGI will not die and a 502 will appear. Bad gateway error.


nginxMain modified parameters
fastcgi_connect_timeout 1800;
fastcgi_send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_buffer_size 1024k;
fastcgi_buffers 3 2 1024k;
fastcgi_busy_buffers_size 2048k;
fastcgi_temp_file_write_size 2048k;
Note: The two 1024k values ​​must be equal , otherwise an error will be reported


The following are the default parameters
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi _busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

----- -------------------------------------------------- ------------------------------------------

[ Author of the article: Wei Shaoqian, please indicate the original link for reprinting: http://mven.cn/nginx-502-504/ ]

1. Error message description:

Nginx 502 Bad Gateway means the requested PHP-CGI It has been executed, but due to some reason (usually a problem with reading resources) it was not completed and the PHP-CGI process was terminated.

Nginx 504 Gateway Time-out means that the requested gateway has not been requested. Simply put, it means that no executable PHP-CGI has been requested.

2. Analysis of the cause of the error message:

Solving these two problems actually requires comprehensive thinking. Generally speaking, Nginx 502 Bad Gateway is related to the setting of php-fpm.conf,

And Nginx 504 Gateway Time-out is related to the settings of nginx.conf.

php-fpm.conf has two crucial parameters, one is "max_children" and the other is "request_terminate_timeout", but this value is not universal and needs to be calculated by yourself.

The calculation method is as follows:

If your server performance is good enough, and the broadband resources are sufficient, and there are no loops or bugs in the PHP script, you can directly set "request_terminate_timeout" to 0s. The meaning of 0s is to let PHP-CGI continue to execute without time limit. And if you can't do this, that is to say, your PHP-CGI may have a BUG, ​​or your bandwidth is not sufficient, or other reasons cause your PHP-CGI to be suspended, then it is recommended that you assign "request_terminate_timeout" A value that can be set based on the performance of your server. Generally speaking, the better the performance, the higher you can set it, anywhere from 20 minutes to 30 minutes. Since my server PHP scripts need to run for a long time, some may take more than 10 minutes, so I set 900 seconds so that PHP-CGI will not die and a 502 will appear. Bad gateway error.

And how is the value of "max_children" calculated? In principle, the larger the value, the better. If there are more php-cgi processes, it will be processed quickly and there will be fewer queued requests. Setting "max_children" also needs to be set according to the performance of the server. Generally speaking, the memory consumed by each php-cgi on a server is about 20M under normal circumstances, so I set my "max_children" to 40, 20M* 40=800M means that at the peak time, the memory consumed by all PHP-CGI is within 800M, which is 1Gb lower than my effective memory. And if my "max_children" is set to a smaller value, such as 5-10, then php-cgi will be "very tired", the processing speed will be very slow, and the waiting time will be longer. If the request has not been processed for a long time, 504 will appear. Gateway Time-out error, and the 502 Bad gateway error will appear if the php-cgi that are being processed very tiredly encounter problems.

3. Temporary solution:

To sum up, the temporary solution to Nginx prompting 502 and 504 errors is:

1. Adjust the relevant settings of php-fpm.conf:

32< ;/value>

30s

2. Adjust the relevant settings of nginx.conf:

fastcgi_connect_timeout 600;
fastcgi_send_ timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 256k;
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;

Fourth, the ultimate solution:

The solution shown in Title 3 will only temporarily solve the problem, while if The number of visits to the website is indeed very large, and Nginx+FastCGI can only have a good effect on handling high concurrency in an instant or a short period of time, so the only ultimate solution at present is: regularly restart php-cgi smoothly.

The specific configuration is as follows:

1. Write a very simple script:

#vi /home/www/scripts/php-fpm.sh

The content is as follows:

#!/bin/bash
# This script run at */1
/usr/local/php/sbin/php-fpm reload

2. Add the script to the scheduled task:

#crontab -e

The content is as follows:

*/1 * * * * /home/www/scripts/php-fpm.sh

Note: To save trouble, you don’t need to write a script. Write the smooth restart command of php-fpm directly into the crontab.

Original article, please indicate when reprinting:

Reprinted from Wei Shaoqian [Zhao Bingliang] – Server System Architecture

The above introduces the ultimate solution to Nginx prompts 502 and 504 errors, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template