加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

python – `uwsgi_modifier1 30`指令没有从PATH_INFO中删除SCRI

发布时间:2020-12-13 21:06:27 所属栏目:Nginx 来源:网络整理
导读:这是我的nginx虚拟主机配置. debian:~# cat /etc/nginx/sites-enabled/myboxserver { listen 8080; root /www; index index.html index.htm; server_name mybox; location /foo { uwsgi_pass unix:/tmp/uwsgi.sock; include uwsgi_params; uwsgi_param SCRIP

这是我的nginx虚拟主机配置.

debian:~# cat /etc/nginx/sites-enabled/mybox
server {
    listen 8080;
    root /www;
    index index.html index.htm;
    server_name mybox;
    location /foo {
        uwsgi_pass unix:/tmp/uwsgi.sock;
        include uwsgi_params;
        uwsgi_param SCRIPT_NAME /foo;
        uwsgi_modifier1 30;
    }
}

这是我的WSGI应用程序的源代码.

debian:~# cat /www/app.py
def application(environ,start_response):
    path_info = script_name = request_uri = None

    if 'PATH_INFO' in environ:
        path_info = environ['PATH_INFO']

    if 'SCRIPT_NAME' in environ:
        script_name = environ['SCRIPT_NAME']

    if 'REQUEST_URI' in environ:
        request_uri = environ['REQUEST_URI']

    output = 'PATH_INFO: ' + repr(path_info) + 'n' + 
             'SCRIPT_NAME: ' + repr(script_name) + 'n' + 
             'REQUEST_URL: ' + repr(request_uri) + 'n'

    start_response('200 OK',[('Content-Type','text/plain')])
    return [output.encode()]

我使用以下两个命令为WSGI应用程序提供服务:

service nginx restart
uwsgi -s /tmp/uwsgi.sock -w app --chown-socket=www-data:www-data

这是我在尝试访问我的Web应用程序时看到的输出.

debian:~# curl http://mybox:8080/foo/bar
PATH_INFO: '/foo/bar'
SCRIPT_NAME: '/foo'
REQUEST_URL: '/foo/bar'

既然我提到了uwsgi_modifier1 30;在我的nginx虚拟主机配置中,我希望PATH_INFO只是’/ bar’,如以下两个URL中所述:

> http://uwsgi-docs.readthedocs.org/en/latest/Nginx.html
> http://blog.codepainters.com/2012/08/05/wsgi-deployment-under-a-subpath-using-uwsgi-and-nginx/

引用第一篇文章中的相关部分:

The uwsgi_modifier1 30 option sets the uWSGI modifier UWSGI_MODIFIER_MANAGE_PATH_INFO. This per-request modifier instructs the uWSGI server to rewrite the PATH_INFO value removing the SCRIPT_NAME from it.

引用第二篇文章中的相关部分:

Standard WSGI request followed by the HTTP request body. The PATH_INFO is automatically modified,removing the SCRIPT_NAME from it.

但我看到我的PATH_INFO保持原样为’/ foo / bar’. SCRIPT_NAME部分,即’/ foo’尚未从中删除.为什么?

最佳答案
读完https://github.com/unbit/uwsgi/pull/19后,我明白使用uwsgi_modifier1 30;已弃用.

所以这就是我解决问题的方法.

首先,我删除了这两行,删除了nginx中的SCRIPT_NAME处理:

    uwsgi_param SCRIPT_NAME /foo;
    uwsgi_modifier1 30;

生成的nginx配置如下所示:

debian:~# cat /etc/nginx/sites-enabled/mybox
server {
    listen 8080;
    root /www;
    index index.html index.htm;
    server_name mybox;
    location /foo {
        uwsgi_pass unix:/tmp/uwsgi.sock;
        include uwsgi_params;
    }
}

然后我重新启动了nginx,并使用像这样的–mount和–manage-script-name选项在uwsgi中处理SCRIPT_NAME.

service nginx restart
uwsgi -s /tmp/uwsgi.sock -w app --chown-socket=www-data:www-data --manage-script-name --mount=/foo=/www/app.py

现在,我得到了预期的输出.

debian:~# curl http://mybox:8080/foo/bar
PATH_INFO: '/bar'
SCRIPT_NAME: '/foo'
REQUEST_URL: '/foo/bar'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读