【发布时间】: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 中所述:
- 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/
引用第一篇文章的相关部分:
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' 尚未从中删除。为什么?
【问题讨论】: