这是我的 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://uwsgi-docs.readthedocs.org/en/latest/Nginx.html
- http://blog.codepainters.com/2012/08/05/wsgi-deployment-under-a-subpath-using-uwsgi-and-nginx/ http://blog.codepainters.com/2012/08/05/wsgi-deployment-under-a-subpath-using-uwsgi-and-nginx/
引用第一篇文章的相关部分:
The uwsgi_modifier1 30
选项设置 uWSGI 修饰符UWSGI_MODIFIER_MANAGE_PATH_INFO
。这个每个请求修饰符指示uWSGI服务器重写PATH_INFO值,并从中删除SCRIPT_NAME。
引用第二篇文章的相关部分:
标准 WSGI 请求后跟 HTTP 请求正文。 PATH_INFO 会自动修改,并从中删除 SCRIPT_NAME。
但我看到我的 PATH_INFO 保持不变'/foo/bar'
。 SCRIPT_NAME 部分,即'/foo'
尚未从中删除。为什么?
看完之后https://github.com/unbit/uwsgi/pull/19 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 中的 SCRIPT_NAME 处理--mount
and --manage-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'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)