Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Solutions to 502 and 504 errors in Nginx

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

A test server in the company has recently encountered 504 and 502 problems: (environment LNMP,php compilation and installation, Fastcgi mode), the problem is not difficult to solve, but here to share your own troubleshooting ideas and ways to deal with the problem.

504 Gateway Time-out,nginx 502 bad gateway

First, analyze the problem:

Nginx 504 Gateway Time-out means that no executable PHP-CGI has been requested.

Nginx 502Bad Gateway means that the requested PHP-CGI has been executed, but the PHP-CGI process is terminated because the read resource and so on are not finished.

Second, the steps of investigation:

1) check whether Nginx can parse php files normally (parsing is normal)

2) View Nginx access log and php log (log error, locking problem)

2.1.1) Nginx related logs

12failed (104: Connection reset by peer) while reading... Timed out (110x Connection timed out) while reading response...

2.1.2) php related logs

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

3) View php and nginx related configurations (Fastcgi related parameters)

.

3.1.1) parameters commonly used in Nginx and Fastcgi:

1234567891011121314151617181920212223242526272829303132333435363738394041fastcgi_connect_timeout 60; # specifies the timeout for connecting to the backend FastCGI. The timeout for fastcgi_send_timeout 60; # to send a request to FastCGI, which refers to the timeout for sending a request to FastCGI after two handshakes have been completed. Fastcgi_read_timeout 300; # timeout for receiving FastCGI replies, which is the timeout for receiving FastCGI replies after two handshakes have been completed. Fastcgi_buffer_size 4k; # specifies how much buffer is needed to read the first part of the FastCGI reply. Generally, the first part of the reply will not exceed 1k. Because the page size is 4k, it is set to 4k here. Fastcgi_buffers 8 4k; # specifies how many and how many buffers are needed locally to buffer FastCGI replies. The default value of fastcgi_busy_buffers_size 8k; # is twice that of fastcgi_buffers. Fastcgi_temp_file_write_size 8k; # how many blocks will be used when writing to fastcgi_temp_path? the default value is twice that of fastcgi_buffers. Fastcgi_cache TEST # turns on FastCGI caching and gives it a name. Fastcgi_cache_valid 200302 1h fastcgifted cacheables valid 301d fastcgifted cachems valid any 1m; # specify the cache time for the specified response code, such as caching 200302 responses for one hour, 301 responses for 1 day, and others for 1 minute. Fastcgi_cache_min_uses 1; # the minimum number of times cached in the fastcgi_cache_path instruction inactive parameter value, as in the above example, if a file is not used once in 5 minutes, then the file will be removed.

3.1.2) View process usage

1netstat-autpn | grep "php-cgi" | wc-l

3.1.3) View Nginx and Fastcgi related configurations

1234567fastcgi_connect_timeout 60 are fastcgivers, sendings, timeouts, 60, fastcgigs, buffers, buffers

According to the previous experience, 300s is enough to adjust the time, and the main setting is the first three:

123fastcgi_connect_timeout 300s, fastcgists, endurance timeout 300s, fastcgists readership timeout 300s

3.1.4) adjust the parameters related to php:

Php.ini

1max_execution_time = 60

Php-fpm.conf:

12request_terminate_timeout=300s# defaults to 0

3.1.4.1) the function of the above parameters

Both items are used to configure the maximum execution time of the PHP script. When this time is exceeded, PHP-FPM terminates not only the execution of the script, but also the Worker process that executes the script. So Nginx finds that the connection it communicates with is disconnected and returns a 502 error to the client.

3.1.4.2) how to configure the parameters:

If the server performance is good enough and the broadband resources are sufficient, you can set "request_terminate_timeout" to 0s directly if the PHP script does not have loops or BUG. 0s means to keep the PHP-CGI running without a time limit.

"max_children" also needs to be set according to the performance of the server. Generally speaking, a server normally consumes about 20m of memory per php-cgi, which can be defined according to its own configuration.

3.1.5) other problems that may exist in 502 and 504

3.1.5.1) max_fail,fail_timeout problem of Nginx

3.1.5.2) Network accidental problem

3.1.5.3) conflict in parameter configuration (eg:php.ini and php-fpm,Nginx configuration Fastcgi specify relevant parameters)

1234567location. Php$ {root htdocs; include fastcgi_params; fastcgi_connect_timeout 60; fastcgi_read_timeout 120; fastcgi_send_timeout 120;}

III. Summary:

4.1According to Ninx related error report, the target can be locked directly according to common error report.

4.2) according to the log, quickly lock the reason. The log is a magic weapon for troubleshooting and must be made full use of.

4.3) adjust the parameters to pay attention to the backup of files to avoid misoperation. In addition, the effective priority of the parameters should be taken into account.

4.4) summarize the problems, take notes and solve them quickly next time you encounter them.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report