In-depth analysis of nginx502 and 504 errors and solutions

Source: Internet
Author: User
Tags fpm ini php script connection reset

When using Nginx, we often encounter 502 Bad Gateway and 504 Gateway Time-out errors. The following uses Nginx + PHP-FPM to analyze the causes and solutions of these two common errors.

1.502 Bad Gateway error

There are two configuration items in php. ini and php-fpm.conf: max_execution_time and request_terminate_timeout.
These two items are used to configure the maximum execution time of a PHP script. When this time is exceeded, php-fpm will not only terminate script execution,
The worker process that executes the script is also terminated. Therefore, nginx will find that the connection to its own communication is disconnected and return the 502 error to the client.

Take php-fpm's request_terminate_timeout = 30 seconds as an example. The details of the error 502 Bad Gateway are as follows:

1) Nginx error access log:

01:09:00 [error] 27600 #0: * 78887 recv () failed (104: Connection reset by peer) while reading response header from upstream,
Client: 192.168.1.101, server: test.com, request: "POST/index. php HTTP/1.1 & Prime;, upstream: "fastcgi: // unix:/dev/shm/php-fcgi.sock :",
Host: "test.com", referrer: "http://test.com/index.php"

2) php-fpm error log:


WARNING: child 25708 exited on signal 15 (SIGTERM) after 21008.883410 seconds from start

Therefore, you only need to increase the values of these two items so that the PHP script will not be terminated because of the long execution time. Request_terminate_timeout can overwrite max_execution_time,
So if you don't want to change the global php. ini, you just need to change the configuration of php-fpm.

In addition, note the max_fail and fail_timeout items in the nginx upstream module. Sometimes the communication between Nginx and upstream servers (such as Tomcat and FastCGI) is broken by accident,
However, if max_fail is set to a relatively small value, nginx will think that the upstream server is down in the next fail_timeout time and will return a 502 error.
Therefore, you can increase max_fail and fail_timeout.

2.504 Gateway Time-out error

The maximum execution time of the script set by php-fpm is long enough, but when the php script is executed, it is found that the nginx error is changed from 502 to 504. Why?
Because we only modify the php configuration, nginx also has the configuration of the communication timeout time with the upstream server:

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;

For example, if the nginx Timeout time is 90 seconds and the php-fpm Timeout time is 300 seconds, the Nginx error access log when the 504 Gateway Timeout error is reported is as follows:

00:55:51 [error] 27600 #0: * 78877 upstream timed out (110: Connection timed out) while reading response header from upstream,
Client: 192.168.1.101, server: test.com, request: "POST/index. php HTTP/1.1 & Prime;, upstream: "fastcgi: // unix:/dev/shm/php-fcgi.sock :",
Host: "test.com", referrer: "http://test.com/index.php"

After you increase the values (mainly read and send), Nginx sets the timeout time to 60 seconds if not configured by default. The 504 error is also fixed.
These three configurations can be configured at the http or server level, or at the location level. If you are worried about affecting other applications, configure them in the location of your own application.

Note:

The following sections take effect for FastCGI:

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;

The following sections take effect for proxy_pass:

Proxy_connect_timeout 90;
Proxy_send_timeout 90;
Proxy_read_timeout 90;

Configuration example:

Location ~ \. Php $ {
Root/home/test.com;
Include fastcgi_params;
Fastcgi_connect_timeout 180;
Fastcgi_read_timeout 600;
Fastcgi_send_timeout 600;
Fastcgi_pass unix:/dev/shm/php-fcgi.sock;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/home/cdai/test.com $ fastcgi_script_name;
}

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.