Nginx: Upstream Timed Out (110: Connection Timed Out) While Reading

Better Stack Team
Updated on October 7, 2024

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:

  1. Open Nginx Configuration:

     
    sudo nano /etc/nginx/nginx.conf
    
  2. Update Timeout Settings:

    Add or adjust proxy_read_timeout, proxy_connect_timeout, and proxy_send_timeout within your location block or upstream block.

  3. Test Configuration:

     
    sudo nginx -t
    
  4. 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

  1. Increase Timeout Settings: Adjust proxy_read_timeout, proxy_connect_timeout, and proxy_send_timeout in Nginx.
  2. Check Upstream Server: Investigate the performance and availability of the upstream server.
  3. Optimize Performance: Scale resources, balance load, and implement caching.
  4. Check Logs: Review logs from both Nginx and the upstream server.
  5. 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.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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 us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build 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.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github