LNMPA encountered 504 Gateway time out error resolution

  • 2020-05-13 04:35:02
  • OfStack

The characteristics of Nginx are static very awesome and Apache are dynamic and stable. The combination of the two is LNMPA, nginx is the front end and apache is the back end. In this way, static processing will be fast and dynamic processing will be stable.

When I thought I was done with the installation, it turned out to be 504 Gateway time-out; At first, I thought it was an accident, but it still happened after many attempts, so I decided to find a solution.

Because 504 Gateway time-out is the exclusive fault of Nginx, so at first I thought it was the configuration error of nginx, so I searched the online tutorial to find the corresponding solution. The following is a brief introduction to the solution of LNMP in this situation. The reason for this error in Nginx is that the limit on the number of PHP-CGI processes is too small. In case of an operation like updating a website, it is very likely that there will be a timeout due to insufficient processes. Therefore, two files related to processing time need to be modified. First is nginx conf file, the file in/nginx conf/nginx conf, after opening, major changes in front of a few time, suggest to modify for more than 120 seconds.

As for what tools to use to modify, recommend online command line editing; Or download it with winscp and modify it with Dreamweaver.


fastcgi_connect_timeout 300s; 
fastcgi_send_timeout 300s; 
fastcgi_read_timeout 300s; 
fastcgi_buffer_size 128k; 
fastcgi_buffers 8 128k;#8 128 
fastcgi_busy_buffers_size 256k; 
fastcgi_temp_file_write_size 256k;

Next, we need to modify the php-fpm.conf file, which is located at/php-5.2.17 /etc/ php-fpm.conf.
Anyway, check the etc folder below the php files after the installation of php. You can edit it online or download it and edit it with Dreamweaver.
After entering the file, search "max_children", which is the number of php-cgi processes. One process accounts for 20M~30M memory.
In addition, search "request_terminate_timeout", which is the timeout time. If the php program needs to operate for a long time, then it is better to set the time 1 point higher, like setting it to 120 seconds, the unit is second by default, or it can be written as 120.

When I set LNMPA to Gateway time-out in accordance with the solution of Nginx, the setting was soon successful, but there would still be 504 Gateway time-out. At this time, I directly switched to LNMP combination and found that I no longer encountered the error of 504 Gateway time-out, which means that the setting has been effective. Why would such a mistake be made?

After searching a lot of data, I finally found out that this is the communication time setting problem between Nginx and Apache. When the user sends a request for php script execution, nginx will not execute php.
This problem will be left to apache in the background, nginx will wait for 1 period of time, apache can process the request of php script in the waiting time, if not, nginx has not received the data request and returned 504 Gateway time-out error, the actual situation is that apache will still process the php script in the background, but the foreground will return an error.

The solution is to modify the communication time between nginx and apche, specifically to configure proxy.conf. This file is under /nginx/conf/, the name may change, for example, some installation packages are laproxy.conf, but it is ** proxy.conf.


proxy_connect_timeout 60; 
proxy_send_timeout 600; 
proxy_read_timeout 90; 
proxy_buffer_size 128k;
proxy_buffers 8 128k; 
proxy_busy_buffers_size 256k; 
proxy_temp_file_write_size 100m; 

Item 1 is the connection time, the front side time after connection, can, within 60 s second is sending time, allowing the back-end data returned, within 90 s, third time is the time to read and write, this is the front for the back-end processing time, this is causing 504 Gateway time - the root cause of out, appear the error, illustrate the value is too small, recommend not less than 600 s, server, so set up longer, ensure the processed.

When I set up this file, restart nginx and apache, and update the article again, I will no longer encounter such errors, indicating that the reason for the error is not nginx, but the short communication time between the front and the back end.

In addition, there will be 502 errors in nginx. In fact, the principle is the same. Set the above part of files, and the combination of LNMPA will still set the communication time between the front and back ends.


Related articles: