Nginx: Upstream Timed Out (110: Connection Timed Out) While Reading
The upstream timed out (110: Connection timed out) while reading
error in Nginx indicates that Nginx was unable to receive a response from the upstream server (e.g., application server or API) within the configured time limit. This error usually occurs when the upstream server is either too slow to respond or is unresponsive.
Here are several steps to diagnose and fix this issue:
1. Increase Timeout Settings
Nginx has several timeout settings that can be adjusted to give the upstream server more time to respond:
proxy_read_timeout
: Defines the maximum time Nginx will wait for a response from the upstream server.proxy_connect_timeout
: Defines the maximum time Nginx will wait to establish a connection to the upstream server.proxy_send_timeout
: Defines the maximum time Nginx will wait to send a request to the upstream server.
Example Configuration:
Edit your Nginx configuration file (e.g., /etc/nginx/nginx.conf
or your specific server block file):
http {
# Other settings...
upstream myapp {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass <http://myapp>;
proxy_read_timeout 120s;
proxy_connect_timeout 120s;
proxy_send_timeout 120s;
}
}
}
Steps:
Open Nginx Configuration:
sudo nano /etc/nginx/nginx.conf
Update Timeout Settings:
Add or adjust
proxy_read_timeout
,proxy_connect_timeout
, andproxy_send_timeout
within yourlocation
block orupstream
block.Test Configuration:
sudo nginx -t
Reload Nginx:
sudo systemctl reload nginx
2. Check Upstream Server Performance
The upstream server might be slow or experiencing issues. Here are some things to check:
- Resource Utilization: Check CPU, memory, and disk usage on the upstream server. High usage can cause delays.
- Application Performance: Analyze the performance of your application or service. Look for slow database queries, inefficient code, or other bottlenecks.
- Server Logs: Review the logs of the upstream server to identify any issues that could be causing delays.
3. Ensure Upstream Server Availability
Ensure that the upstream server is reachable and not experiencing downtime or network issues.
Ping/Connectivity: Check if you can reach the upstream server from the Nginx server.
ping backend1.example.com
Network Configuration: Ensure that there are no firewall rules or network issues blocking access.
4. Optimize Upstream Server
If your upstream server is under heavy load or inefficient, consider:
- Scaling: Increase the number of backend servers or resources allocated to the existing ones.
- Load Balancing: Distribute traffic more evenly across multiple servers.
- Caching: Implement caching to reduce the load on the upstream server. Use Nginx’s caching features or a dedicated caching layer.
5. Check Nginx Logs
Review Nginx’s error logs for more details on the timeout issue:
sudo tail -f /var/log/nginx/error.log
6. Adjust Upstream Server Timeout
If the upstream server itself has timeout settings (e.g., in a web application server like Apache, Tomcat, or a database server), ensure those settings are compatible with Nginx’s timeout settings.
Summary
- Increase Timeout Settings: Adjust
proxy_read_timeout
,proxy_connect_timeout
, andproxy_send_timeout
in Nginx. - Check Upstream Server: Investigate the performance and availability of the upstream server.
- Optimize Performance: Scale resources, balance load, and implement caching.
- Check Logs: Review logs from both Nginx and the upstream server.
- Adjust Upstream Settings: Ensure upstream server timeouts are aligned with Nginx settings.
By following these steps, you should be able to address the upstream timed out
error and ensure smoother interactions between Nginx and your upstream servers.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github