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, orlocationblocks, which makes it ineffective. - Confusing
proxy_read_timeoutwithproxy_connect_timeoutorproxy_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
| Directive | Purpose | Default Value |
|---|---|---|
| proxy_read_timeout | Timeout for reading response from proxied server | 60s |
| proxy_connect_timeout | Timeout for establishing connection to proxied server | 60s |
| proxy_send_timeout | Timeout for sending request to proxied server | 60s |
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.