121

On a local development machine, I have a nginx reverse proxy like so:

server {
  listen 80;
  server_name myvirtualhost1.local;
  location / {
    proxy_pass http://127.0.0.1:8080;
}

server {
  listen 80;
  server_name myvirtualhost2.local;
  location / {
    proxy_pass http://127.0.0.1:9090;
}

Yet if I debug my application, the response may be delayed for an infinite amount of time, yet after 30 seconds I get:

504 Gateway Time-out

as a response.

How can I disable the timeout and have my reverse proxy wait forever for a response? And I like the setting to be global, so that I do not have to set it for each proxy.

1
  • 1
    Consider starting a background job and letting the user check its status later. Commented May 19, 2016 at 12:39

4 Answers 4

142

It may not be possible to disable it at all, yet a feasible workaround is to increase the execution time. On a nginx tutorial site, it was written:

If you want to increase time-limit for all-sites on your server, you can edit main nginx.conf file:

vim /etc/nginx/nginx.conf

Add following in http{..} section

http {
     fastcgi_read_timeout 300;
     proxy_read_timeout 300;
}

and reload nginx' config:

sudo service nginx reload

I have used a rather large value that is unlikely to happen, i.e. 999999 or using time units, to one day via 1d.

Beware that setting the value to 0 will cause a gateway timeout error immediately.

13
  • 11
    @kb. It's funny as that I am the OP and just posted the most workable workaround as an answer, while waiting for a real solution ^^
    – k0pernikus
    Commented Oct 17, 2016 at 8:41
  • 2
    Haha, I completely missed that you were OP. But your answer is the correct one, you could make it even more clear (for future googlers like me) that there is no way to disable it. =)
    – kb.
    Commented Oct 17, 2016 at 10:21
  • 6
    Thanks for the info about 0 not working! Note that you can specify time units with readable suffixes, so you could use a value like 1d.
    – Brandon
    Commented Aug 30, 2017 at 20:30
  • 4
    I have added both that and proxy_connect_timeout 600; to the nginx.conf file, but the timeout is still on 60 seconds. Anything else I should try?
    – andreszs
    Commented Oct 27, 2017 at 0:32
  • 2
    I guess you've been downvoted because you should never touch /etc/nginx/nginx.conf. In case of update, this file might be erased, and anyway it's a bad practice. Read the file carefully and you'll see in the http section, it includes include /etc/nginx/conf.d/*.conf;. So, this is where you should put your own "custom" configuration: create a file, like /etc/nginx/conf.d/global_custom.conf (or whatever name you want), and put your directives there. You can find so many misleading informations on the web... Commented May 31, 2022 at 10:34
41

It is possible to increase the timeout for nginx, to add to @k0pernikus 's answer, the following can be added to your location block:

        location /xyz {
         proxy_read_timeout 1800;
         proxy_connect_timeout 1800;
         proxy_send_timeout 1800;
         send_timeout 1800;
        }

Here 1800 is in seconds.

After changing the config, verify the syntax with:

nginx -t -c /some/path/nginx.conf

Then reload nginx with:

nginx -s reload
3
  • 5
    about each timeout nginx.org/en/docs/http/… Commented Apr 15, 2020 at 7:02
  • is there anyway to set a variable as 1800 and then assign that variable, I tried using set, but it didnt work Commented Aug 7, 2020 at 20:12
  • 1
    @MaxCarroll, I don't think config files generally support variables. Set wouldn't work as it is not bash. Commented Aug 9, 2020 at 10:58
28

If you are using AWS, and Load Balancer, you should edit Idle timeout. I think default is 60 seconds

8
  • I understand why this was down voted but maybe if the answer was updated to explain its use case, it would be more useful. Although this doesn't go to answer the OP, it is something useful to consider if you are using ELB as there is a limit on how long they will keep a connection open also.
    – doz87
    Commented Oct 2, 2018 at 5:04
  • @doz87 yes, it's just something for consideration
    – szeljic
    Commented Oct 2, 2018 at 8:28
  • This is really worth checking especially on my case that working with Magento under the AWS. Good point @szeljic Commented Oct 30, 2018 at 8:23
  • 1
    This deserves all the upvotes - any AWS users will still be limited to 60 seconds regardless of their NGINX settings without this
    – Aphire
    Commented Jun 13, 2019 at 8:50
  • 1
    @foo it's on the LoadBalancer page on AWS. the Description tab and the Attributes section. Idle timeout.
    – szeljic
    Commented Aug 26, 2020 at 13:23
12

I've been fighting with nginx 502 timeout error and could not solve the issue. However, it happened to be gunicorn causing the timeout error. So you might also need to check your fastcgi settings.

For gunicorn it's:

gunicorn wsgi:application --timeout 300

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .