How to fix 504 gateway time out Nginx

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?
  • conclusions

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;
READ  Installation of Ubuntu 16.04 LTS Server Edition

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:

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:

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:

READ  Deploy Your First Docker Container

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,

More information can sometimes be seen in the 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:

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.

Conclusions

In this article, we examined how to fix 504 gateway timeout issue faced in Nginx, and also why this error may occur.

Authored By Imran Yousaf

I am Imran Yousaf, a computer geek, founder of the site Smashinglab.com. I am a die hard fond of open-source software and Linux operating system. In addition to Linux, I am interested in everything related to information technology and modern science.