How to Use proxy_connect_timeout in Nginx for Connection Timing
proxy_connect_timeout directive in the Nginx configuration to set the maximum time Nginx waits to establish a connection with an upstream server. It is specified in seconds or time units like ms, s, m. For example, proxy_connect_timeout 10s; sets a 10-second timeout.Syntax
The proxy_connect_timeout directive sets the timeout for establishing a connection with the upstream server. It accepts a time value with units like ms (milliseconds), s (seconds), or m (minutes).
This directive is used inside the http, server, or location blocks.
- proxy_connect_timeout: The directive name.
- time: The timeout duration (e.g., 5s, 100ms).
proxy_connect_timeout 5s;Example
This example shows how to set proxy_connect_timeout to 10 seconds inside a location block. It tells Nginx to wait up to 10 seconds when connecting to the upstream server before giving up.
http {
server {
listen 80;
location / {
proxy_pass http://backend_server;
proxy_connect_timeout 10s;
}
}
}Common Pitfalls
One common mistake is setting proxy_connect_timeout too low, causing Nginx to give up on connections prematurely, leading to errors for slow upstream servers.
Another mistake is confusing proxy_connect_timeout with proxy_read_timeout or proxy_send_timeout, which control different timeout phases.
Always set proxy_connect_timeout based on expected network latency and upstream server response times.
# Wrong: Too low timeout causing connection failures proxy_connect_timeout 500ms; # Right: Reasonable timeout for slow connections proxy_connect_timeout 10s;
Quick Reference
- proxy_connect_timeout: Time to wait for connection to upstream server.
- Use time units like
s(seconds),ms(milliseconds). - Set inside
http,server, orlocationblocks. - Does not affect data transfer timeouts.