0
0
NginxHow-ToBeginner · 3 min read

How to Use proxy_read_timeout in Nginx for Connection Timeouts

Use the proxy_read_timeout directive in your Nginx configuration to set the time Nginx waits for a response from a proxied server. It defines how long Nginx will wait to read data after establishing a connection. Set it inside a location, server, or http block to control timeout behavior.
📐

Syntax

The proxy_read_timeout directive sets the timeout for reading a response from the proxied server. It accepts a time value with units like seconds (s), minutes (m), or hours (h).

It is used inside http, server, or location blocks.

  • proxy_read_timeout: The directive name.
  • time: Timeout duration (e.g., 60s, 2m).
nginx
proxy_read_timeout 60s;
💻

Example

This example shows how to set proxy_read_timeout to 90 seconds inside a location block. It tells Nginx to wait up to 90 seconds for a response from the backend server before timing out.

nginx
server {
    listen 80;
    server_name example.com;

    location /api/ {
        proxy_pass http://backend_server;
        proxy_read_timeout 90s;
    }
}
Output
Nginx will wait up to 90 seconds for a response from http://backend_server before closing the connection due to timeout.
⚠️

Common Pitfalls

Common mistakes when using proxy_read_timeout include:

  • Setting the timeout too low, causing premature connection closures for slow backend responses.
  • Placing the directive outside http, server, or location blocks, which makes it ineffective.
  • Confusing proxy_read_timeout with proxy_connect_timeout or proxy_send_timeout, which control different timeout phases.
nginx
location /api/ {
    proxy_pass http://backend_server;
    # Wrong: timeout outside location or server block
}

# Correct usage:
location /api/ {
    proxy_pass http://backend_server;
    proxy_read_timeout 60s;
}
📊

Quick Reference

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

Key Takeaways

Set proxy_read_timeout inside http, server, or location blocks to control backend response wait time.
Use a timeout value that matches your backend response speed to avoid premature disconnections.
proxy_read_timeout only controls reading response; use proxy_connect_timeout and proxy_send_timeout for other phases.
Incorrect placement of proxy_read_timeout makes it ineffective.
Default proxy_read_timeout is 60 seconds if not set explicitly.