How to increase timeout for NGINX?

23,477

Solution 1

Add the following directives at the end of the 'http' section to increase the timeout limit to 180 seconds (3 minutes):

http {
    <...>
    include /etc/nginx/conf.d/.conf;

    proxy_send_timeout 180s;
    proxy_read_timeout 180s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;
}

Source : https://support.plesk.com/hc/en-us/articles/115000170354-An-operation-or-a-script-that-takes-more-than-60-seconds-to-complete-fails-on-a-website-hosted-in-Plesk-nginx-504-Gateway-Time-out

Solution 2

I faced the same problem .. I've found a workaround telling nginx to accept a certain amount of data over the default one. Not trying to manage the timeout itself but changing the amount of data accepted in the transaction did the trick.

 server {

        client_max_body_size            5M; # or more ^^

}

but it's really not a secured option .. it works, but take care doing this.

moreover if you are using a reverse proxy WSGI gateway (Php for example) .. the underlayrer mechanism may take precedence over that

Share:
23,477
Adam Strike
Author by

Adam Strike

Updated on July 09, 2022

Comments

  • Adam Strike
    Adam Strike almost 2 years

    I am using Python, Flask, uWSGI and NGINX to host a web server. One of the functions involves generating a file for the user which can take up to a minute or two. On this action I keep getting a 504 timeout from NGINX. I tried to change some config variables in /etc/nginx/nginx.conf like keepalive_timeout but that didn't work.I also tried to add the following to /etc/nginx/conf.d/timeout.conf:

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

    and then I reloaded with systemctl reload nginx but it didn't change anything.

    How do I increase the length of time before the request times out? Thanks for any help

  • Adam Strike
    Adam Strike about 5 years
    What do you mean that it's not a secured option? Thanks for help btw :)
  • Adam Strike
    Adam Strike about 5 years
    And would I put that in /etc/nginx/nginx.conf?
  • Emmanuel BRUNET
    Emmanuel BRUNET about 5 years
    accepting a large file to be downloaded is the same as accepting a large file uploading using this option ^^ .. may be a binary one .. with auto excecutable capabillities .. NGINX won't check that
  • Emmanuel BRUNET
    Emmanuel BRUNET about 5 years
    yes in the /etc/nginx/nginx.conf in your sever section as stated
  • Adam Strike
    Adam Strike about 5 years
    It's not a large file, just take a few minutes to generate?
  • Emmanuel BRUNET
    Emmanuel BRUNET about 5 years
    to generate ? you must understand that a web server only transfers / parses http / https or other protocols frames . it does not "generate" data .. and should not !
  • Adam Strike
    Adam Strike about 5 years
    I don't have a server section in nginx.conf and I get an error when trying to add one. I do have multiple server section in my /sites-available/myappname, but that does not fix the problem.
  • Emmanuel BRUNET
    Emmanuel BRUNET about 5 years
    difficult for me to check that .. without the source config code and without knowing your own environment (that you should not provide) . you have an include statement in your root configuration file .. just follow it and you'll find the right server configuration to apply what I proposed. By default in /etc/nginx/conf.d as I remember
  • Adam Strike
    Adam Strike about 5 years
    Where exactly would I add this?
  • Adam Strike
    Adam Strike about 5 years
    My conf.d file is empty and the web server doesn't generate the file, it's a flask application. :)
  • Emmanuel BRUNET
    Emmanuel BRUNET about 5 years
  • Satish Patro
    Satish Patro about 5 years
    you have not added timeout in your answer
  • Ricardo Vargas
    Ricardo Vargas almost 3 years
    Finally a solution that Works.