Tips / Nginx

How to Fix 504 Gateway Timeout using Nginx

It is very common to see a 504 Gateway Timeout error using Nginx webserver. This timeout error is generated often by a number of reasons on the backend connection that is serving content. To fix 504 Gateway Time-out, you will have to figure out what configuration are you using.

How you might see the 504 Gateway Timeout error

Different websites may customize the 504 gateway timeout error message. Here are the most common 504 error messages:

  • “504 Gateway Timeout”
  • “504 Gateway Time-Out”
  • “504 Gateway Timeout NGINX”
  • “Nginx 504 Gateway Timeout”
  • “HTTP 504 Gateway Timeout”
  • “HTTP 504 Error”
  • “HTTP 504”
  • “Gateway Timeout (504)”

504 Gateway Timeout error on Nginx + FastCGI (php-fpm)

For Nginx + FastCGI (php-fpm), you should try to tweak nginx configuration in this way:

Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):

max_execution_time = 300

But, you should also change set request_terminate_timeout parameter (commented by default) at www.conf file from PHP-FPM:

pico -w /etc/php-fpm.d/www.conf

Then set the variable to the same value as max_execution_time:

request_terminate_timeout = 300

Now let’s add fastcgi_read_timeout variable inside our Nginx virtual host configuration:

location ~ .php$ {
root /var/www/sites/;
try_files $uri =404;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;

Then restart nginx:

service nginx reload

504 Gateway Timeout error using Nginx as Proxy

For Nginx as Proxy for Apache web server, this is what you have to try to fix the 504 Gateway Timeout error:

Add these variables to nginx.conf file:

  proxy_connect_timeout       600;
  proxy_send_timeout          600;
  proxy_read_timeout          600;
  send_timeout                600;

Then restart nginx:

service nginx reload

Additional Resources

Popular search terms:

  • 504 gateway time-out nginx
  • 504 gateway timeout nginx
  • nginx gateway timeout
  • nginx timeout

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Hello,

    I have linux server and I am getting this “504 Gateway Time-out” error on my website. Actually I am Windows user and I am not understanding linux. I can’t fix it this error. Could you write more detail please how can I do that?


  • Pablo

    Thank you very much! I was having this issue and your post was exactly what I needed to fix this 😉

  • Suman Kar

    Solution worked like a magic…thanks

  • NicoVS

    Nice info…
    Just a little tip: service nginx reload should be sufficient. This way you will not abruptly disconnect your clients

  • admin

    You are right on that NicoVS. Fixed.

    • Just a note, you updated “service nginx reload” on the “For nginx as Proxy” section, but not the FastCGI section of your article.

      • admin

        Fixed, thanks!

  • Do you have to set
    proxy_connect_timeout 600;

    from nginx docs

    “This directive assigns a timeout for the connection to the upstream server. It is necessary to keep in mind that this time out cannot be more than 75 seconds. ”

    So the upper bound is 75 no?

  • admin

    Haven’t seen any issues setting the value to more than 75.

  • Stefan

    Thank you for posting this, it was very helpful!

  • Thank you for usefull information, It’s help me to right configure Nginx Server on Ubuntu. And now Nginx works great:-)

  • shadownet

    Don’t forget to restart php also:
    service php5-fpm reload

    (not working otherwise for me)

  • Hey there!

    Is there a way to prevent this timeout if I do not have access to the php.ini file? Maybe from the php file or .htaccess?


  • Thanks for this. May I make the suggestion though of covering where the Nginx virtual host configuration file is normally? I know everybody’s install is usually different but gotta admit that it took me a few minutes to remember where mine was. For me, it was in the /etc/nginx/sites-available directory.

    • admin


      You mentioned ‘normally’, but what is normal to you using /etc/nginx/sites-available (usually on Debian/Ubuntu) is not normal for RHEL based users like Fedora, CentOS and RHEL itself. And it’s also different when you build nginx from the source gz files. It’s impossible to cover all posible nginx directory installations.

      Thanks for your comment.

  • annoyed

    it’s sad that this is the top search result for “nginx timeout 504″…i guess the ONLY way to use nginx is with PHP…otherwise “you will have to figure out what configuration are you using”. WTF. useless.

  • Did not work for me. Got this error:
    “proxy_connect_timeout” directive is not allowed here in /etc/nginx/nginx.conf:10

    None of those commands were allowed.

  • Does anyone know how to set “fastcgi_read_timeout” to never timeout? I set it to 0 (zero) and it will display “service unavailable”. Thanks.

    • Try setting it to a very high value, example 1 hour or 1 day:

      Good luck.

  • Thanks for that, worked like a charm!

  • Shreya Mahajan

    i didn’t get nginx directory in etc
    please let me know as soon as possible,thanks in advance

  • Abraham Tugalov

    Works fine.

  • Thanks a lot!

  • Thanks for sharing really helpful

  • rosscado

    For my setup (nginx proxy with a gunicorn server and Python WSGI app) the important setting was `proxy_read_timeout`. I needed to increase this setting to a value greater than the app was taking to compute its responses.

  • Svnet Libre

    It works with Nginx for Yunohost Internet Cube A20-OLinuXino-LIME2 Great Thanks

  • vk


    We are getting 504 NGINX gateway time out error for the first time and for 2nd time it opens fine while hitting the URL . Can anyone respond how to fix this ?

  • Gangsar Swa Purba

    a million thank’s! 🙂

  • Thanks a lot

  • Alex

    This can lead to a denial of service as the sockets are held open, use with caution.

  • mohamad bathulah

    Thanks a lot !

  • Sathiya Moorthy Karan

    Thanks a lot, the post was very good one, also please take look at my site and

  • Michael

    doesn’t seem to be correct. In my config for the fpm (pool.d/www.conf) is the following comment

    ; 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
    ;request_terminate_timeout = 0

    so leave this config alone if you don’t want your process to be killed