Nginx 502 and 504 error solutions

  
                  

The following is a summary of the method please refer to I. Error description:

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but for some reason (usually reading resources) Problem) The PHP-CGI process is terminated without execution.

Nginx 504 Gateway Time-out means that the requested gateway does not request it, in short, there is no request for PHP-CGI to execute.

Second, the error prompt reason analysis:

Solving these two problems is actually a comprehensive thinking, in general, Nginx 502 Bad Gateway and php-fpm.conf settings,

The Nginx 504 Gateway Time-out is related to the setting of nginx.conf.

php-fpm.conf has two crucial parameters, one is ”max_children”, the other is ”request_terminate_timeout” , but this value is not generic, but needs to be calculated by itself.

The calculation is as follows:

If your server performance is good enough and the bandwidth resources are sufficient, PHP scripts are not looping or buggy, 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 broadband is not enough or other reasons cause your PHP-CGI to be suspended, then you are advised to give ”request_terminate_timeout” Assign a value that can be set based on the performance of your server. Generally speaking, the better the performance, the higher you can set, 20 minutes -30 minutes. Since my server PHP script needs to run for a long time, some may exceed 10 minutes, so I set 900 seconds, which will not cause PHP-CGI to die and 502 Bad gateway error.

And ”max_children” How is this value calculated? In principle, the larger the value, the better. The php-cgi process will process very quickly, and there will be very few queued requests. Setting ”max_children” also needs to be set according to the performance of the server. Generally speaking, the normal memory consumption of each php-cgi is about 20M, so my "max_children” I set it to 40, 20M*40=800M means that at the peak, all PHP-CGI consumes less than 800M, which is lower than my effective memory of 1Gb. And if my ”max_children” is set smaller, such as 5-10, then php-cgi will be "very tired", the processing speed is also very slow, and the waiting time is longer. If the request is not processed for a long time, the 504 Gateway Time-out error will occur, and the php-cgi that is being processed very tired will have the error 502 Bad gateway if there is a problem.

Third, the temporary solution:

In summary, Nginx prompt 502 and 504 error temporary solution is:

1, adjust php-fpm.conf Related settings:

<value name="max_children">32</value> <value name="request_terminate_timeout">30s</value>

2, adjust nginx.conf related settings:

server_tokens off; sendfile on; tcp_nopush on; server_names_hash_bucket_size 256; client_header_buffer_size 256k; large_client_header_buffers 4 256k; client_body_buffer_size 256k; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; client_max_body_size 50m; keepalive_timeout 120? #fastcgi_connect_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;

fastcgi_inte rcept_errors on; 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;

IV Solution: A, crontab written in php-fpm smooth restart command B Increase the value of children appropriately and reduce the value of max_requests. It is recommended that the average cloud host's children be at 5-10, max_requests at 1000-2000, and adjust it by itself. By the way, reducing the value of max_requests can shorten the period of php-cgi restart. The problem caused by accidental high memory operation will also be shortened.

Copyright © Windows knowledge All Rights Reserved