【问题标题】:The `uwsgi_modifier1 30` directive is not removing the SCRIPT_NAME from PATH_INFO as documented`uwsgi_modifier1 30` 指令没有从 PATH_INFO 中删除 SCRIPT_NAME,如记录
【发布时间】:2014-05-03 17:47:20
【问题描述】:

这是我的 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

这是我尝试访问我的网络应用程序时看到的输出。

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

由于我在我的 nginx 虚拟主机配置中提到了 uwsgi_modifier1 30;,我希望 PATH_INFO 仅为 '/bar',如以下两个 URL 中所述:

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

uwsgi_modifier1 30 选项设置 uWSGI 修饰符 UWSGI_MODIFIER_MANAGE_PATH_INFO。这个 per-request 修饰符指示 uWSGI 服务器重写 PATH_INFO 值,从中删除 SCRIPT_NAME。

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

标准 WSGI 请求后跟 HTTP 请求正文。 PATH_INFO 会自动修改,并从中删除 SCRIPT_NAME。

但我看到我的 PATH_INFO 保持不变为'/foo/bar'。 SCRIPT_NAME 部分,即'/foo' 尚未从中删除。为什么?

【问题讨论】:

    标签: python nginx wsgi uwsgi


    【解决方案1】:

    阅读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 并在 uwsgi 中使用 --mount--manage-script-name 这样的选项进行 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'
    

    【讨论】:

    • 因为uwsgi_modifier1 30 只是与PATH_INFO 相同,你可能只是uwsgi_param PATH_INFO /bar;
    猜你喜欢
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2017-12-23
    相关资源
    最近更新 更多