The introduction

Usually when we visit the website, or request some interfaces in the work, or online service alarm, we often see 502 and 504 these two status codes. If it is operation and maintenance students received 502 or 504 problem feedback, usually the first thing is to restart the service, often good, restart dafa is undefeatable, ha ha ha. In this article, we’ll look at when these two status codes occur and how to solve them

Meaning of 502 and 504

502 Bad Gateway

Gateway error More officially, a server working as a gateway or proxy tries to execute a request and receives an invalid response from an upstream server

504 Gateway Time-out

Gateway timeout Officially, a server working as a gateway or proxy tries to execute a request and does not receive a response in time from either an upstream server (identified by A URI, such as HTTP, FTP, LDAP) or a secondary server (such as DNS)

The above explanations are more abstract, and the following examples are used to explain the causes of the two states, so as to understand the meaning of the two state codes

Causes 502 and 504

502 Bad Gateway

First of all, we all know that status codes beginning with 5 are server-side problems. In life, we will meet such a situation, we in the Spring Festival ticket, found 502, or a star hot search, a blog 502 situation. Collodially, this is actually because the website suddenly has a large number of requests to come in, the other side of the server can not be handled in time, resulting in 502

So technically, when a request comes in from a web page, nginx sends the request to phP-fpm for processing, and for some reason, the request hasn’t completed, and the process that handled the request terminates

This situation is related to the configuration of PHP-fpm. Note the following three configurations:

In the php.ini max_execution_time
This sets the maximum time inseconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from  tying up the server. The default setting is 30. When running PHP from thecommand line the default setting is 0.



The official document: https://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

Copy the code

This parameter sets the maximum amount of time (in seconds) the script is allowed to run before the parser terminates. This helps prevent improperly written scripts from occupying server resources. The default setting is 30. When running PHP from the command line, the default setting is 0. So when the request does not work for 30 seconds, it is aborted and we receive error 502

Request_terminate_timeout in PHP – FPM. Conf
; The timeout for serving a single request after which the worker process will

; be killed. This option should be used when the 'max_execution_time' ini option

; does not stop script execution for some reason. A value of '0' means 'off'.

; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)

; Default Value: 0

Copy the code

If max_execution_time fails to terminate the script for some reason, phP-fpm will kill the request. If max_execution_time fails to terminate the script for some reason, phP-fpm will kill the request

Max_children in PHP – FPM. Conf

The meaning of this configuration was explained in the previous article (for a thorough understanding of CGI, FastCGI, PHP-Fpm), and it represents the maximum number of child processes that can survive at the same time. When a large number of requests come in during a period of time, resulting in the maximum number of PHP-FPM responses, the number of subsequent requests will be 502. You can use the following command to view the current number of connections

#netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'



A sample result is returned:

LAST_ACK 5 (number of requests waiting to be processed)

SYN_RECV 30 

ESTABLISHED 1597 (Normal data transmission status)

FIN_WAIT1 51 

FIN_WAIT2 504 

TIME_WAIT 1057



Status: Description

CLOSED: No connection is active or ongoing

LISTEN: The server is waiting for an incoming call

SYN_RECV: A connection request has arrived and is awaiting confirmation

SYN_SENT: The application has started and a connection has been opened

ESTABLISHED: Indicates the normal data transmission status

FIN_WAIT1: The application says it is finished

FIN_WAIT2: The other side has agreed to release

ITMED_WAIT: waits for all groups to die

CLOSING: Try to close both sides simultaneously

TIME_WAIT: THE other side has initialized a release

LAST_ACK: waits for all groups to die



From: https://www.cnblogs.com/linyouyi/p/10108440.html

Copy the code

In this case, you need to check if there are a lot of locked processes in the database. In this case, you need to check if there are a lot of locked processes in the database. This is when a database deadlock causes a timeout and you need to restart the database or kill the SQL process that caused the deadlock

504 Gateway Time-out

504 is generally related to nginx configuration. For example, if nginx does not receive a response from phP-fpm within the timeout period set by nginx, it will return 504 to the client. When the fastCGI buffer is too small, the FastCGI process is suspended and a 504 error occurs

The following configuration parameters are involved in nginx:

fastcgi_connect_timeout

Meaning: Defines the timeout period used to establish a connection to the FastCGI server



fastcgi_send_timeout

Meaning: Sets the timeout period for transferring requests to the FastCGI server. The timeout is set only between two consecutive write operations and is not used for transmission of the entire request. If the FastCGI server does not receive any information within this time, the connection will be closed



fastcgi_read_timeout

Meaning: Defines the timeout period used to read the response from the FastCGI server. Timeouts are set only between two consecutive reads and are not used to transmit the entire response. If the FastCGI server does not transfer anything during this time, the connection will be closed



fastcgi_buffer_size

Meaning: Set size to the buffer used to read the first part of the response received from the FastCGI server. This section usually contains a small response header. By default, the buffer size is equal to one memory page



fastcgi_buffers

Meaning: Sets the number and size of the buffer used to read responses from the FastCGI server for a single connection. By default, the buffer size is equal to one memory page



fastcgi_busy_buffers_size

Meaning: When caching is enabled for responses from the FastCGI server, limit the total size of the buffer, which can be busy sending the response to the client and the response is not yet fully read. Meanwhile, the rest of the buffer can be used to read the response and buffer part of the response into a temporary file if needed. By default, size is limited to the size of the two buffers set by the FastcGI_BUFFer_SIZE and fastCgi_Buffers directives



fastcgi_temp_file_write_size

fastcgi_intercept_errors



More than you may refer to the official document: http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html

Copy the code