nginx + uwsgi + flask - 禁用自定义错误页面 [英] nginx + uwsgi + flask - disabling custom error pages

查看:174
本文介绍了nginx + uwsgi + flask - 禁用自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以禁用nginx的自定义错误页面 - 如果我可以调用它 - 显示我的框架的异常页面?

我真的不能看到我的werkzeug调试器工具呈现在HTML ...
$ b $ p更新



好的,我得到使一个非常简单的烧瓶应用程序的工作,我会张贴:



/home/my_user/.virtualenvs/nginx-test/etc/ nginx.conf

  worker_processes 1; 
events {worker_connections 1024; }
http {
server {
listen 5000;
server_name localhost;
access_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/access.log;
error_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/error.log;

位置/ {
包含uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;


$ b code
$ b / home / my_user / dev / nginx_test / ___ init ___。py

  from flask import Flask 

app = Flask(__ name__)

@ app.route('/')
def index():
抛出异常()
$ b $如果__name__ = ='__main__':
app.run('0.0.0.0',debug = True)

$ P $
$ b

 $ echo $ PYTHONPATH 
/ home / my_user / dev /

如何运行uwsgi:

  $ uwsgi -s /tmp/uwsgi.sock --module nginx_test --callable app 

如何运行nginx:
$ b $ pre $ $ $ $ $ $ $ nginx -c〜/ .virtualenvs / nginx-test /etc/nginx.conf -p〜 /.virtualenvs/nginx-test/lib/nginx/

如果我点击根页面:





如果我ru n nginx手动类似:

pre $ py $ / $ / $ / $ c>

我会看到,而我想看到:



当然,我确信它会在我没有引发异常的时候工作,但是在我的index()函数中返回了Hello World。

这个在.NET中被称为自定义错误页面。我想禁用它,让nginx / uwsgi直接将调试器生成的html传递给浏览器,而不是内部的服务器错误。

UPDATE 2

现在,如果我更改我的烧瓶应用程序以启用调试模式:

/ home / my_user / dev / nginx_test / ___ init ___。py

  from flask import Flask 

app = Flask(__ name__)
app.config.update(DEBUG = True)
@ app.route('/')
def index():
异常)

if __name__ =='__main__':
app.run('0.0.0.0',debug = True)

然后我得到502错误。

但是如果我反而引发异常:

/ home / my_user / dev / nginx_test / ___ init ___。py

  from flask import Flask 
$ b app = Flask(__ name__)
app.config.update(DEBUG = True)
@ app.route('/')
def index():
return'Hello World'

if __name__ =='__main__':
app.run('0.0.0.0',debug = True)

当我打开页面时,我的浏览器上显示Hello World( http:// localhost:5000 )。

解决方案

这个Internal Server Error从烧瓶。它在调试模式关闭的情况下执行



uwsgi将您的代码作为模块导入,不作为脚本运行。 __ name__ =='__main __'为False,if语句不执行。尝试在if之外设置调试模式:

  app = Flask(__ name__)
app.debug = True $ b然而,强烈建议永不 离开服务器上的调试模式公共互联网,因为用户可以让服务器运行任何代码。这是一个严重的安全问题。


Is it possible to disable nginx's custom error pages - if I may call them that - to display my framework's exception pages?

I can't really see my werkzeug debugger tool rendered in html...

UPDATE

OK, I got to make a very very simple flask application to work and I'll post the bits:

/home/my_user/.virtualenvs/nginx-test/etc/nginx.conf

worker_processes 1;
events { worker_connections 1024; }
http {
        server {
                listen 5000;
                server_name localhost;
                access_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/access.log;
                error_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/error.log;

                location / {
                        include uwsgi_params;
                        uwsgi_pass unix:/tmp/uwsgi.sock;
                }
        }
}

/home/my_user/dev/nginx_test/___init___.py

from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
    app.run('0.0.0.0', debug=True)

PYTHONPATH environment variable:

$ echo $PYTHONPATH
/home/my_user/dev/

How I run uwsgi:

$ uwsgi -s /tmp/uwsgi.sock --module nginx_test --callable app

How I run nginx:

$ nginx -c ~/.virtualenvs/nginx-test/etc/nginx.conf -p ~/.virtualenvs/nginx-test/lib/nginx/

If I hit the root page:

If I run nginx manually like:

python /home/my_user/dev/nginx_test/___init___.py

I will see instead, and what I want to see:

Of course I made sure it would work when I didn't raise the exception, but returned 'Hello World' for example on my index() function.

This is referred to custom error pages in .NET. I want to disable this and let nginx/uwsgi pass the html generated by the debugger directly to the browser instead of the internal server error thing.

UPDATE 2

Now if I change my flask app to enable debugging mode by:

/home/my_user/dev/nginx_test/___init___.py

from flask import Flask

app = Flask(__name__)
app.config.update(DEBUG=True)
@app.route('/')
def index():
    raise Exception()

if __name__ == '__main__':
    app.run('0.0.0.0', debug=True)

Then I get 502 error.

But if I instead of raise Exception:

/home/my_user/dev/nginx_test/___init___.py

from flask import Flask

app = Flask(__name__)
app.config.update(DEBUG=True)
@app.route('/')
def index():
    return 'Hello World'

if __name__ == '__main__':
    app.run('0.0.0.0', debug=True)

I get 'Hello World' on my browser when I hit the page (http://localhost:5000).

解决方案

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.

这篇关于nginx + uwsgi + flask - 禁用自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆