提问者:小点点

“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;
        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\u修饰符130在我的nginx虚拟主机配置中,我希望路径信息仅为'/bar',如下两个URL中所述:

  • http://uwsgi-docs.readthedocs.org/en/latest/Nginx.html

引用第一条的相关部分:

uwsgi\u修饰符130选项设置uwsgi修饰符uwsgi\u修饰符\u管理路径\u信息。此按请求修饰符指示uWSGI服务器重写路径信息值,从中删除脚本名称。

引用第二条的相关部分:

标准WSGI请求,后跟HTTP请求主体。将自动修改路径信息,并从中删除脚本名称。

但是我看到我的路径信息保持不变,如'/foo/bar'。脚本名称部分,即'/foo'尚未从中删除。为什么?


共1个答案

匿名用户

读后https://github.com/unbit/uwsgi/pull/19我理解使用uwsgi_修饰符130已弃用。

这就是我解决问题的方法。

首先,我删除SCRIPT_NAME处理在nginx删除这两行:

    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--ement-cript-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'