Mastering Nginx Timeouts: Complete Guide to All Timeout Settings
This article explains every Nginx timeout directive, shows practical configuration examples for client, proxy, FastCGI and other timeouts, and provides best‑practice recommendations to improve server stability and performance across different application scenarios.
Key Points
Setting timeouts in Nginx prevents long‑running connections from consuming resources, improving availability and response speed. The main timeout directives are: client_header_timeout: timeout for reading the client request header. client_body_timeout: timeout for reading the client request body. send_timeout: timeout for sending a response to the client. proxy_connect_timeout: timeout for establishing a connection to an upstream server. proxy_read_timeout: timeout for reading data from an upstream server. proxy_send_timeout: timeout for sending a request to an upstream server.
Example Configuration
Below is a sample Nginx configuration illustrating these directives:
http {
server {
listen 80;
# client timeouts
client_header_timeout 10s;
client_body_timeout 15s;
# response timeout
send_timeout 20s;
location / {
proxy_pass http://upstream_server;
# proxy timeouts
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
proxy_send_timeout 25s;
}
}
}Introduction
Nginx is a high‑performance web and reverse‑proxy server that offers many timeout options to optimise connection management and resource utilisation.
1. Overview of Nginx Timeout Settings
The timeout settings are grouped as follows:
2. Client‑Side Timeouts
2.1 client_header_timeout
Timeout for reading the client request header (default 60s).
http {
client_header_timeout 30s;
}2.2 client_body_timeout
Timeout for reading the client request body (default 60s).
http {
client_body_timeout 30s;
}2.3 send_timeout
Timeout for sending a response to the client (default 60s).
http {
send_timeout 30s;
}3. Proxy‑Related Timeouts
3.1 proxy_connect_timeout
Timeout for establishing a connection to the upstream server (default 60s).
location / {
proxy_connect_timeout 10s;
proxy_pass http://backend;
}3.2 proxy_read_timeout
Timeout for reading a response from the upstream server (default 60s).
location / {
proxy_read_timeout 30s;
proxy_pass http://backend;
}3.3 proxy_send_timeout
Timeout for sending a request to the upstream server (default 60s).
location / {
proxy_send_timeout 30s;
proxy_pass http://backend;
}4. FastCGI Timeouts
4.1 fastcgi_connect_timeout
Timeout for connecting to a FastCGI server (default 60s).
location ~ \.php$ {
fastcgi_connect_timeout 10s;
fastcgi_pass 127.0.0.1:9000;
}4.2 fastcgi_read_timeout
Timeout for reading a response from FastCGI (default 60s).
location ~ \.php$ {
fastcgi_read_timeout 30s;
fastcgi_pass 127.0.0.1:9000;
}4.3 fastcgi_send_timeout
Timeout for sending a request to FastCGI (default 60s).
location ~ \.php$ {
fastcgi_send_timeout 30s;
fastcgi_pass 127.0.0.1:9000;
}5. Other Important Timeouts
5.1 keepalive_timeout
Timeout for keeping a client connection alive (default 75s).
http {
keepalive_timeout 65s;
}5.2 resolver_timeout
Timeout for DNS resolution (default 30s).
location / {
resolver 8.8.8.8;
resolver_timeout 10s;
proxy_pass http://$host$request_uri;
}5.3 lingering_timeout
Timeout for waiting for remaining data after a client closes the connection.
http {
lingering_timeout 20s;
}6. Best‑Practice Recommendations
Production‑grade values
Proxy timeouts: 10‑30 seconds
Client timeouts: 30‑60 seconds
keepalive_timeout: 15‑30 seconds
Adjust by application type
API services: shorter timeouts (5‑15 seconds)
File uploads: longer timeouts (300 seconds+)
Monitoring & Tuning
7. Full Configuration Example
http {
# client timeouts
client_header_timeout 30s;
client_body_timeout 30s;
send_timeout 30s;
# keepalive
keepalive_timeout 30s;
# proxy timeouts
proxy_connect_timeout 15s;
proxy_read_timeout 30s;
proxy_send_timeout 30s;
# FastCGI timeouts
fastcgi_connect_timeout 15s;
fastcgi_read_timeout 30s;
fastcgi_send_timeout 30s;
# other
resolver_timeout 10s;
lingering_timeout 20s;
server {
listen 80;
location / {
proxy_pass http://backend;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
}
}
}8. Conclusion
Nginx offers a rich set of timeout options; configuring them wisely can greatly boost server stability and performance. Adjust values based on your specific workload and network conditions, and continuously refine them through monitoring.
9. Final Thoughts
With the explanations and diagrams provided, you should now understand how to use each Nginx timeout directive. Remember that there is no one‑size‑fits‑all setting—optimal values are determined by testing and monitoring in your environment.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
