The ultimate solution for Nginx prompting 502 and 504 errors

Source: Internet
Author: User
: This article mainly introduces the ultimate solution for Nginx prompts 502 and 504 errors. if you are interested in PHP tutorials, please refer to it. Large memory can increase the following parameters to effectively reduce error 502


Main modification parameters in php-fpm
128 // Each resource consumes about 20 MB of memory and is set based on the memory size.
1024 // If each max_children process exceeds this number, it will be automatically killed and will be automatically rebuilt later. Generally, around 1000 is set.
0 s // If your server has good performance and sufficient bandwidth resources, you can directly set "request_terminate_timeout" to 0 s if the PHP script has no loops or bugs. 0 s means to keep the PHP-CGI running without a time limit. And if you can't do that, that is, your PHP-CGI may have a BUG, or if your bandwidth is insufficient or your PHP-CGI may be suspended for other reasons, we recommend that you assign a value to "request_terminate_timeout", which can be set based on the performance of your server. In general, the better the performance, the higher you can set, 20 minutes-30 minutes can be done. Since my server PHP script takes a long time to run, some may take more than 10 minutes, so I set 900 seconds, this will not cause the PHP-CGI to die and the 502 Bad gateway error occurs.


Main modification parameters in nginx
Fastcgi_connect_timeout 1800;
Fastcgi_send_timeout 1800;
Fastcgi_read_timeout 1800;
Fastcgi_buffer_size 1024 k;
Fastcgi_buffers 32 1024 k;
Fastcgi_busy_buffers_size 2048 k;
Fastcgi_temp_file_write_size 2048 k;
Note: the two k values must be equal; otherwise, an error is returned.


The following are default parameters:
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64 k;
Fastcgi_buffers 4 64 k;
Fastcgi_busy_buffers_size 128 k;
Fastcgi_temp_file_write_size 128 k;

Bytes -------------------------------------------------------------------------------------------------

[Author: Wei Shaoqian reproduced please indicate the original link: http://mven.cn/nginx-502-504/]

1. Error message description:

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but the PHP-CGI process is terminated for some reason (generally because the problem with reading resources) is not completed.

Nginx 504 Gateway Time-out means that the requested Gateway does not have a request. Simply put, it does not have a request for PHP-CGI that can be executed.

II. cause analysis of error messages:

To solve these two problems is actually need to think about, in general, Nginx 502 Bad Gateway and php-fpm.conf settings,

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

The php-fpm.conf has two crucial parameters: "max_children" and "request_terminate_timeout", but this value is not generic and needs to be calculated on its own.

The calculation method is as follows:

If your server has good performance and sufficient bandwidth resources, you can directly set "request_terminate_timeout" to 0 s if the PHP script does not have loops or bugs. 0 s means to keep the PHP-CGI running without a time limit. And if you can't do that, that is, your PHP-CGI may have a BUG, or if your bandwidth is insufficient or your PHP-CGI may be suspended for other reasons, we recommend that you assign a value to "request_terminate_timeout", which can be set based on the performance of your server. In general, the better the performance, the higher you can set, 20 minutes-30 minutes can be done. Since my server PHP script takes a long time to run, some may take more than 10 minutes, so I set 900 seconds, this will not cause the PHP-CGI to die and the 502 Bad gateway error occurs.

How is the value of "max_children" calculated? In principle, the larger the value is, the better. if the php-cgi process is too large, it will process very quickly and there will be very few requests in the queue. "Max_children" also needs to be set based on the server performance. generally, a server normally consumes about 20 MB of memory per php-cgi, so my "max_children" I set to 40, 20 M * 40 = 800M that is to say that at the peak of all PHP-CGI consumption exists within M, less than my effective memory 1 Gb. If my "max_children" settings are small, such as 5-10, php-cgi will be "very tired", the processing speed will be slow, and the waiting time will be long. If a request is not processed for a long Time, the error 504 Gateway Time-out occurs, if the php-cgi that is working very well encounters a problem, the 502 Bad gateway error will occur.

III. temporary solution:

To sum up, the temporary solutions for Nginx prompting 502 and 504 errors are as follows:

1. adjust the settings of the php-fpm.conf:

32

30 s

2. adjust the nginx. conf settings:

Fastcgi_connect_timeout 600;
Fastcgi_send_timeout 600;
Fastcgi_read_timeout 600;
Fastcgi_buffer_size 256 k;
Fastcgi_buffers' 16 256 k;
Fastcgi_busy_buffers_size 512 k;
Fastcgi_temp_file_write_size 512 k;

IV. end-level solutions:

The solution shown in Heading 3 can only temporarily solve the problem, and if the website traffic is indeed very large, nginx + FastCGI can only achieve good results in handling high concurrency in an instant or a short period of time. Therefore, the only ultimate solution currently is to regularly and smoothly restart php-cgi.

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: For the sake of convenience, you can directly write the php-fpm smooth restart command in crontab without writing a script.

Original article, reprinted please note:

Reproduced from Wei Shaoqian [Zhao Bingliang]-server system architecture

The above describes the ultimate solution for Nginx error 502 and 504, including some content, and hope to help those who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.