What is solution of "120 upstream prematurely closed connection while reading response header from upstream error "??

I am making flask app which is binded gunicorn .As i open my page i get 502 Bad gatway error and tail has given this error log - *120 upstream prematurely closed connection while reading response header from upstream.What to do??

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

A 502 error could be caused by a number of things, and without more information about how you’ve configured your application it is hard to get to the root of your specific issue. The tutorial “How To Troubleshoot Common HTTP Error Codes” gives you a good place to start debugging your problem:

The 502 status code, or Bad Gateway error, means that the server is a gateway or proxy server, and it is not receiving a valid response from the backend servers that should actually fulfill the request.

If the server in question is a reverse proxy server, such as a load balancer, here are a few things to check:

  • The backend servers (where the HTTP requests are being forwarded to) are healthy
  • The reverse proxy is configured properly, with the proper backends specified
  • The network connection between the backend servers and reverse proxy server is healthy. If the servers can communicate on other ports, make sure that the firewall is allowing the traffic between them
  • If your web application is configured to listen on a socket, ensure that the socket exists in the correct location and that it has the proper permissions

The additional context (upstream prematurely closed connection) suggests that this might be caused by Gunicorn timing-out before successfully responding to the request. By default, Gunicorn is set to time out after 30 seconds. Raising that value might help debug the issue. This can be done by passing --timeout when starting it. See the Gunicorn docs for more info.

For more information about getting a Flask app up and running with Gunicorn and Nginx, check out: