这是我的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 modifierUWSGI_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’尚未从中删除.为什么?
首先,我删除了这两行,删除了Nginx中的SCRIPT_NAME处理:
uwsgi_param SCRIPT_NAME /foo;
uwsgi_modifier1 30;
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'