logo of Akmatori
18.05.2024

Understanding and Fixing "Upstream Prematurely Closed Connection" in Nginx

head-image

Seeing "upstream prematurely closed connection" in Nginx? This error means the upstream server closed the connection too soon. Here’s how to fix it.

Common causes of "upstream prematurely closed connection" in Nginx

  • Upstream server errors

    • Server crashes
    • Misconfigurations
  • Timeouts

    • FastCGI, Proxy, or Upstream timeouts
  • Buffer and size limitations

    • Inadequate buffer sizes
    • Header size limits
  • Application issues

    • Bugs or errors in the application on upstream server

How to fix the "upstrem prematurely closed connection" error

  • Adjust Nginx timeouts

    • Increase proxy_read_timeout
    • Increase fastcgi_read_timeout
    proxy_read_timeout 90s;
    fastcgi_read_timeout 90s;
    
  • Increase Buffers

    • Adjust proxy_buffer_size
    • Modify proxy_buffers
    proxy_buffer_size 16k;
    proxy_buffers 4 16k;
    

Use this example to adjust your Nginx settings:

http {
    proxy_read_timeout 90s;
    proxy_connect_timeout 90s;
    proxy_send_timeout 90s;

    server {
        location / {
            proxy_pass http://upstream_server;
            proxy_buffer_size 16k;
            proxy_buffers 4 16k;
            proxy_busy_buffers_size 32k;
        }
    }
}

Upstream server errors

Upstream server crashes can cause this error. Check the server logs for clues. Fix any found issues and restart the server. Alse check CPU and memory usage on upstream server.

  • Check logs on upstream servers

    • Look at Application logs
    • Check upstream system server logs
    dmesg -T
    journalctl -xe --since '3 hours ago'
    
  • Check server resources on upstream servers

    • Check CPU usage
      top
      
    • Monitor memory usage
      free -h
      

Conclusion

Fixing the "upstream prematurely closed connection" error is crucial for smooth operation. By following these steps, you can troubleshoot and resolve the issue.

If you ready to optimize your Nginx server and ensure seamless connections than try Akmatori, a Globally Distributed TCP/UDP Load Balancer. Akmatori enhances performance and reliability, ensuring no premature connection closures. Join our community and take your web performance to the next level with Akmatori today!

Maximize your website or application's performance and reliability!