pass-by-ref
pass-by-ref

Reputation: 1298

nginx gives 502 for non handled exception 500 error in flask

I am running a Flask application and using uwsgi socket nginx configuration.

This may be a stupid question but the issue I am facing is that whenever there is exception raised in Flask code (non handled exception for example 1/0), my nginx gives 502 instead of 500. I wanted to know if raising the exception is not getting propagated to nginx as 500 from uwsgi unix socket by default or do I need to specify this explicitly? Somewhere I read that for exceptions, Flask doesn't raise 500 error message automatically. Any comments will be helpful. Thanks.

Upvotes: 0

Views: 1641

Answers (2)

pass-by-ref
pass-by-ref

Reputation: 1298

Finally I was able to solve this issue. Thanks to @Jeff Storey and @joppich.

My proxy server was not able to read the response from by backend server which was causing this issue. I added an interceptor to catch all exceptions and propagate it to NGINX via uwsgi using the nginx proxy_intercept_errors on;. Big thanks Jeff and joppich. Appreciate your help.

Upvotes: 0

saj
saj

Reputation: 681

Look at the 502 spec:

The HyperText Transfer Protocol 502 Bad Gateway server error response code indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.

If your app responds anything while hitting an exception, it's most likely garbage and nginx raises (correctly) the 502, meaning "I won't (as opposed to can't) talk to the backend".

If you want 500s, you have to catch any possible underlying exception, wrap it in a valid response and it will be processed by nginx.

Upvotes: 3

Related Questions