Problem discovery
Recently, I was doing tests and found that basically all interfaces have too long response time error feedback is 504.
analyze
Nginx access shows 504 Gateway Time-out, which is usually due to the long execution time of the program, which causes the response time to time out. For example, the program needs to execute 90 seconds, while the maximum response waiting time of nginx is 30 seconds, which will cause a timeout.
Usually the following situations cause
- The program is processing a large amount of data, causing the wait timeout.
- An external request is called in the program, and the external request response timeout.
- The connection to the database failed without stopping, and the dead loop reconnected.
If this happens, we can first optimize the program and shorten the execution time. If it is a task that is time-consuming, such as file parsing, you can increase the parameters of nginx timeout limit so that the program can be executed normally.
Modify nginx configuration
In the middle, set the following parameters to increase the timeout time
http { ... fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; ... }
fastcgi_connect_timeout
Fastcgi connection timeout, default 60 seconds
fastcgi_send_timeout
The timeout time of the nginx process sending requests to the fastcgi process, the default value is 60 seconds
fastcgi_read_timeout
The timeout time of the fastcgi process sending output process to the nginx process, the default value is 60 seconds.
server { listen 8888; location / { proxy_pass http://pdfs; proxy_connect_timeout 18000; proxy_send_timeout 18000; proxy_read_timeout 18000; } }
proxy_connect_timeout 1800s;#nginx and backend server connection timeout timeout (proxy connection timeout)
proxy_send_timeout 1800s;# Backend server data return time (proxy send timeout)
proxy_read_timeout 1800s;# After the connection is successful, the backend server response time (proxy reception timeout)
This is the end of this article about Nginx solving the 504 timeout problem. For more information about Nginx 504 timeout, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!