NGINX Timeouts: How To Fix 504 Gateway Timeout Error

Overview:

This guide explains the key various timeout settings in NGINX. It also shows how to fix the 504 gateway timeout error in NGINX.

Foreword

NGINX offers several timeout parameters depending on the upstream server or framework it is working with. For example, if NGINX is passing requests to another server, then you can define timeouts using settings provided by the ngx_http_proxy_modulemodule.

If NGINX is passing requests to a FastCGI server, then you can define timeouts using the settings provided in the ngx_http_fastcgi_modulemodule. The same applies to the SCGI server, uwsgi server, and memcached server.

Also, check out: HAProxy Timeouts: How To Fix 504 Gateway Timeout Error

NGINX Key Timeout Parameters

As I mentioned earlier, NGINX timeout parameters are defined according to the upstream server to which NGINX is passing requests. This section explains the timeout settings for the different servers. It starts with the proxy timeout settings.

Proxy Server Timeout Settings

If NGINX is passing requests to another server such as Node.js, then you can use the following timeout parameters:

  • proxy_connect_timeout: this parameter is used to set a timeout for establishing a connection with a proxied server. A value of at most 75 seconds is recommended. This setting is applicable in the http, server and location context.
  • proxy_read_timeout: this setting defines the timeout for reading a response from the proxied server. This setting is applicable in the http, server and location context.
  • proxy_send_timeout: this parameter defines a timeout for transmitting a request to the proxied server. This setting is applicable in the http, server and location context.
FastCGI Server Timeout Settings

For a FastCGI server, you can set the following parameters:

  • fastcgi_connect_timeout: this parameter is used to set a timeout for establishing a connection with a FastCGI server. A value of at most 75 seconds is recommended. This setting is applicable in the http, server and location context.
  • fastcgi_read_timeout: this parameter sets a timeout for reading a response from the FastCGI server. This setting is applicable in the http, server and location context.
  • fastcgi_send_timeout: this parameter defines a timeout for transmitting a request to the FastCGI server. This setting is applicable in the http, server and location context.
Uwsgi Server Timeout Settings

For a uwsgi server, define the following settings:

  • uwsgi_connect_timeout: this parameter is used to set a timeout for establishing a connection with a uwsgi server. A value of at most 75 seconds is recommended. You can define this setting in the http, server and location context.
  • uwsgi_read_timeout: this setting defines a timeout for reading a response from the uwsgi server. You can define this setting in the http, server and location context.
  • uwsgi_send_timeout: this parameter defines a timeout for transmitting a request to the uwsgi server. You can define this setting in the http, server and location context.
SCGI Server Timeout Settings

For an SCGI server, use the following settings:

  • scgi_connect_timeout: this parameter is used to set a timeout for establishing a connection with a SCGI server. A value of at most 75 seconds is recommended. This setting works in the http, server and location context.
  • scgi_read_timeout: this setting defines a timeout for reading a response from the SCGI server. This setting works in the http, server and location context.
  • scgi_send_timeout: this parameter defines a timeout for transmitting a request to the SCGI server. This setting works in the http, server and location context.
Memcached Server Timeout Settings

For a memcached server, you can set the following parameters:

  • memcached_connect_timeout: this parameter is used to set a timeout for establishing a connection with a memcached server. A value of at most 75 seconds is recommended. It is applicable in the http, server and location context.
  • memcached_read_timeout: this setting defines a timeout for reading a response from the memcached server. It is applicable in the http, server and location context.
  • memcached_send_timeout: this parameter defines a timeout for transmitting a request to the memcached server. It is applicable in the http, server and location context.

Take note that:

  • For all the *_read_timeout settings, the timeout is applied only between two successive read operations, not for the transmission of the whole response. If the respective upstream server does not transmit anything within this time, the connection is closed.
  • Also, for all the *_send_timeout settings, the timeout is applied only between two successive write operations, not for the transmission of the whole response. If the respective upstream server does not transmit anything within this time, the connection is closed.

Another useful timeout parameter is send_timeout. It is provided in the ngx_http_core_module module. It is used to define the timeout for transmitting a response to the client. It is applicable in the http, server and location context.

Besides, in NGINX time intervals are specified with these suffixes: ms – milliseconds, s – seconds, m – minutes, h – hours, d – days, w – weeks, M – months, 30 days, and y – years, 365 days.

Fix 504 Gateway Timeout Error

If you are experiencing the 504 gateway timeout error, for example, while using NGINX to proxy requests to Node.js, you can define the above proxy timeout parameters in your NGINX configuration like this.

Below is a sample configuration showing proxy timeout settings defined in the /api location context.

upstream test-app-ui {
    server 10.10.1.1:31001;
    server 10.10.1.2:31001;
    server 10.10.1.3:31001;
}

upstream api-service {
    server 10.10.1.1:31000;
    server 10.10.1.2:31000;
    server 10.10.1.3:31000;
}

server {
    listen 80;
    server_name  fossguidetest.app;
    client_max_body_size 10M;

    location / {
        proxy_pass http://test-app-ui;
    }

    location /api {
        proxy_pass http://mak-api-service;
        proxy_set_header X-Real-IP $http_x_forwarded_for; #$remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For  $http_x_forwarded_for;

        # Following is necessary for Websocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_connect_timeout 60s;
        proxy_read_timeout 1800s;
        proxy_send_timeout 1800s;
        send_timeout 1800s;
    }
}

This image shows key NGINX proxy timeout settings.

Sample NGINX proxied server timeout settings

Important: You need to set timeout values accordingly based on the requirements of your applications or services that you are serving using NGINX. Some operations may require very high timeout values.

Conclusion

That’s all I prepared for you about NGINX timeout parameters and how to fix the 504 gateway timeout error. What is your take on this topic/guide? Share your thoughts with me via the comment section below. You can also find additional information on the NGINX official websites, links are provided below under references.

References:

1. https://nginx.org/en/docs/

2. https://www.nginx.com/

You may also like...

8 Responses

  1. Hi there just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different web browsers and both show the
    same results.

  2. Thanks very interesting blog!

  3. You have made some really good points there. I checked on the net for additional information about the issue and found
    most individuals will go along with your views on this web site.

  4. Hi my loved one! I want to say that this post is amazing, great written and come with
    almost all important infos. I would like to see more posts like
    this .

Leave a Reply

Your email address will not be published. Required fields are marked *

Page Contents