How to increase timeout for NGINX?
Asked Answered
S

3

12

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

Splenius answered 17/2, 2019 at 21:28 Comment(0)
A
18

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

Adler answered 23/2, 2020 at 8:6 Comment(1)
Finally a solution that Works.Transcendentalistic
C
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

Constantinople answered 17/2, 2019 at 22:26 Comment(10)
What do you mean that it's not a secured option? Thanks for help btw :)Splenius
And would I put that in /etc/nginx/nginx.conf?Splenius
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 thatConstantinople
yes in the /etc/nginx/nginx.conf in your sever section as statedConstantinople
It's not a large file, just take a few minutes to generate?Splenius
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 !Constantinople
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.Splenius
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 rememberConstantinople
My conf.d file is empty and the web server doesn't generate the file, it's a flask application. :)Splenius
Let us continue this discussion in chat.Constantinople
C
-5
server {

        server_name                     yourhost;
        client_max_body_size            5M;

        location / {
                proxy_http_version      1.1;
                proxy_set_header        Host $host;
                proxy_set_header        Upgrade $http_upgrade;
                proxy_set_header        Connection "upgrade";
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_pass              http://127.0.0.1:8080; # depending on your network conf

        }
}
Constantinople answered 17/2, 2019 at 22:55 Comment(2)
Where exactly would I add this?Splenius
you have not added timeout in your answerBoigie

© 2022 - 2024 — McMap. All rights reserved.