The Nginx web server is a full-blown web server that has great potential to deal with huge traffic and especially Reddit type effect. Apart from a web server, Nginx can also be configured as a proxy server. And this configuration is one of the famous and widely used features. For example, when working with php-fpm and other dynamic language modules.
When Nginx is configured to act as proxy server, in such mode one could face an error 504 gateway timeout Nginx. In our today’s article, we will try to understand why it arises and how to deal with it. We will discuss several solutions and reasons.
This article will cover:
- How to fix 504 gateway timeout Nginx?
Good advice: What is RAID 0, RAID 1, RAID 5 and RAID 10. Read More →
What does the 504 gateway timeout mean?
As I said, this error occurs when the Nginx server is running in proxy server mode. For example, using Apache as backend server and Nginx as front-end proxy server. Literally, it means that the response time from the server has been exceeded. In our case, the response time from php-fpm has been exceeded. Consider several reasons for this behavior:
- The PHP script or in another language is completely frozen and will not return any response;
- The script runs for a very long time, but in Nginx, the interval is set to reset the connection if the target server did not respond to the request for the allocated rows;
- The server is overloaded and does not have time to serve all clients, return the answers to all Nginx requests;
Next, consider what you can do if you encountered a 504 gateway timeout error Nginx.
How to fix 504 gateway timeout Nginx
The first option – it is when your server, php-fpm or Apache has not enough system resources such as memory or CPU. You can view free memory with the command free:
The load on the processor can be observed by the top command:
Naturally, if you see that PHP takes up all of the CPU time, it means a problem in the server resources. You can investigate the issue and optimize certain settings to reduce the response time.
The second option is if it was planned for the script to work for a long time. In this case, you need to configure Nginx to wait for a response from Apache or php-fpm. To solve the problem in the case of php-fpm, you just need to add two lines to the FastCGI configuration block:
fastcgi_send_timeout 300; fastcgi_read_timeout 300;
Here 300 means 300 seconds, for most scripts, this is quite enough, but you can further increase the value if necessary. Also, error 504 can occur when Nginx is used as a proxy for Apache or any other web server, then you still need to configure the timeout for the proxy. Add these lines to the server section:
proxy_connect_timeout 600; proxy_send_timeout 600; Proxy_read_timeout 600; Send_timeout 600;
Here we already set a timeout of 600 seconds for various kinds of actions – connecting, sending data, reading data and so on. After the Nginx setup is complete, restart it:
sudo systemctl restart nginx
The last option that we will consider is the script freezes. If you execute the script yourself, you will immediately see that it has performance issues due to some ill or bad coding or maybe non-compatibility with php version. If such error occurs it is a more serious problem. You need to immediately read the error log and trace out the problem. Grep is very helpful in such situations,
grep -i "504" /var/log/nginx/access.log
More information can sometimes be seen in the error.log:
grep -i "504" /var/log/nginx/error.log
Further, if the problem is in php-fpm, you can track which scripts are executed slowly using the built-in function slow-log. To activate it, add the following lines to the configuration of your pool:
sudo vi /etc/php-fpm.d/www.conf slowlog = /var/log/php-fpm/www-slow.log request_slowlog_timeout = 5s
Here 5 seconds, means that scripts will be added to the log file, which runs for more than five seconds. You can change this value at your discretion. In the log, you can see not only the scripts themselves but could also trace the root cause that led to such problems.
Then you just have to figure out what to do with it, for example, optimize scripts or disable extra plug-ins.
In this article, we examined how to fix 504 gateway timeout issue faced in Nginx, and also why this error may occur.