118

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.

k0pernikus
  • 4,170
  • 4
  • 17
  • 17

4 Answers4

139

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.

k0pernikus
  • 4,170
  • 4
  • 17
  • 17
  • I'm not the downvoter but a wild guess would be that OP asked for a way to disable the timeout and the reply just says how to increase it. So it's not an answer to the question OP asked. – kb. Oct 17 '16 at 07:16
  • 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 Oct 17 '16 at 08: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. Oct 17 '16 at 10:21
  • 6
    Thanks for the info about `0` not working! Note that [you can specify time units with readable suffixes](https://nginx.org/en/docs/syntax.html), so you could use a value like `1d`. – Brandon Aug 30 '17 at 20:30
  • 3
    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 Oct 27 '17 at 00:32
  • When giving a solution please make it so that it applies generally, instead of your specific scenario. fastcgi_read_timeout is not always applicable, for example, Elastic Beanstalk apps on AWS. – Syed Priom Oct 10 '18 at 16:16
  • I have the same issue as andreszs, exactly 60 seconds. – Luc Apr 01 '19 at 22:37
  • Update: adding `proxy_read_timeout` fixed the issue. @andreszs In case it's still relevant to you, could you confirm if that fixes it for you as well? – Luc Apr 03 '19 at 21:01
  • @BrandonMintern Thank you very much for the hint about the time units, I have added that to my answer. – k0pernikus Apr 04 '19 at 15:47
  • Thank you very much! I've been trying different approaches for the past few hours, my problem was with fastcgi_read_timeout. Cheers! – Julien B. Aug 26 '20 at 15:32
  • Is it possible to set different timeout values for specific reverse proxies, rather than setting them globally? – JoeMjr2 Oct 24 '21 at 17:58
  • 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... – Olivier Pons May 31 '22 at 10:34
  • 1
    Maybe you didn't know but you can change globally (`http {...}`), or by virtual host (`server {...}`), or even by URLs (`location {...}`). – Olivier Pons May 31 '22 at 10:36
40

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
Vasantha Ganesh
  • 501
  • 4
  • 4
28

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

szeljic
  • 391
  • 3
  • 5
  • 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 Oct 02 '18 at 05:04
  • @doz87 yes, it's just something for consideration – szeljic Oct 02 '18 at 08:28
  • This is really worth checking especially on my case that working with Magento under the AWS. Good point @szeljic – MuntingInsekto Oct 30 '18 at 08:23
  • Thanks bro, this works for me since I use AWS load balancer for distributing to my EC2 instance – Vũ Thành Tâm Dec 05 '18 at 07:47
  • 1
    This deserves all the upvotes - any AWS users will still be limited to 60 seconds regardless of their NGINX settings without this – Aphire Jun 13 '19 at 08:50
  • This was something I ran into. I needed to step back and think about all the components in my stack before I realised the LB was the reason for the timeout. – user38238 Jul 03 '20 at 15:08
  • "idle timeout" : what field would that be, and where is it? – foo Aug 19 '20 at 19:30
  • 1
    @foo it's on the LoadBalancer page on AWS. the Description tab and the Attributes section. Idle timeout. – szeljic Aug 26 '20 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
Kostyantyn
  • 221
  • 2
  • 4