0
0
NginxHow-ToBeginner · 3 min read

How to Set Proxy Timeout in Nginx: Simple Guide

To set proxy timeout in Nginx, use the proxy_connect_timeout, proxy_send_timeout, and proxy_read_timeout directives inside your server or location block. These control how long Nginx waits to connect, send, and receive data from the proxied server.
📐

Syntax

The main proxy timeout directives in Nginx are:

  • proxy_connect_timeout: Time to wait for a connection to the proxied server.
  • proxy_send_timeout: Time to wait for sending a request to the proxied server.
  • proxy_read_timeout: Time to wait for a response from the proxied server.

Each directive accepts a time value like 30s (30 seconds) or 1m (1 minute).

nginx
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
💻

Example

This example shows how to set proxy timeouts in an Nginx server block to wait up to 30 seconds for connection, sending, and reading from the proxied server.

nginx
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_connect_timeout 30s;
        proxy_send_timeout 30s;
        proxy_read_timeout 30s;
    }
}
⚠️

Common Pitfalls

Common mistakes when setting proxy timeouts include:

  • Setting timeouts too low, causing premature disconnections.
  • Forgetting to set all three timeouts, which can lead to unexpected behavior.
  • Placing timeout directives outside the location or server block where they have no effect.

Always test your configuration after changes to avoid downtime.

nginx
location / {
    proxy_pass http://backend_server;
    # Wrong: timeout directives outside location block
}

# Correct placement:
location / {
    proxy_pass http://backend_server;
    proxy_connect_timeout 30s;
    proxy_send_timeout 30s;
    proxy_read_timeout 30s;
}
📊

Quick Reference

DirectivePurposeDefault Value
proxy_connect_timeoutTimeout for establishing connection to proxied server60s
proxy_send_timeoutTimeout for sending request to proxied server60s
proxy_read_timeoutTimeout for reading response from proxied server60s

Key Takeaways

Use proxy_connect_timeout, proxy_send_timeout, and proxy_read_timeout to control proxy timeouts in Nginx.
Set these directives inside the server or location block for them to work.
Choose timeout values that balance waiting for slow servers and avoiding hanging connections.
Test your Nginx configuration after changes to ensure proper behavior.
Avoid setting timeouts too low to prevent premature connection drops.