nginx + uwsgi + flask - disabling custom error pages

谁说我不能喝 提交于 2019-12-01 03:25:38

This "Internal Server Error" page is not from nginx but from Flask. It does so when debug mode is off.

uwsgi is importing your code as a module, not running at as a script. __name__ == '__main__' is False and the if statement is not executed. Try setting debug mode outside of the if:

app = Flask(__name__)
app.debug = True

However, it is strongly recommended to never leave the debug mode on a server on the public internet, since the user can make the server run any code. This is a serious security issue.

Use Flask#errorhandler to register your own error handlers in flask. For example to replace the 404 you would do something like:

app = Flask()

@app.errorhandler(404)
def handel_404(error):
    return render_template('404.html')

Simon Sapin has really given you the correct answer. You need to enable debug in Flask. Nginx does not return any custom error pages unless you explictly configure it to do so.

If you use the following code you will see your debug messages from flask, proxied via Nginx.

from flask import Flask

app = Flask(__name__)
app.debug = True

@app.route('/')
def index():
    raise Exception()

As per your update 2. You are seeing a 502 (bad gateway) because Flask is simply not returning any response at all, or a response that Nginx does not understand. A 502 is not a problem with Nginx. It means that whatever Nginx is trying to talk (your flask app in this case) is not working properly at all.

However, in many ways you shouldn't be doing this. Debugging mode should only be enabled when you are running flask on your local dev machine. Which is the whole point of the if __name__ == "__main__": line anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!