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

Posted February 5, 2018 23.5k views
PythonPython FrameworksUbuntu 16.04

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??

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.

Submit an Answer
1 answer

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:

by Mitchell Anicas
This guide focuses on identifying and troubleshooting the most commonly encountered HTTP error codes, i.e. 4xx and 5xx status codes, from a system administrator's perspective. There are many situations that could cause a web server to respond to a request with a particular error code--we will cover common potential causes and solutions.