Lon*_*ner 9 python wsgi nginx uwsgi
这是我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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()]
Run Code Online (Sandbox Code Playgroud)
我使用以下两个命令为WSGI应用程序提供服务:
service nginx restart
uwsgi -s /tmp/uwsgi.sock -w app --chown-socket=www-data:www-data
Run Code Online (Sandbox Code Playgroud)
这是我在尝试访问我的Web应用程序时看到的输出.
debian:~# curl http://mybox:8080/foo/bar
PATH_INFO: '/foo/bar'
SCRIPT_NAME: '/foo'
REQUEST_URL: '/foo/bar'
Run Code Online (Sandbox Code Playgroud)
由于我uwsgi_modifier1 30;在我的nginx虚拟主机配置中提到过,我期望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'尚未从中删除.为什么?
Lon*_*ner 12
在阅读https://github.com/unbit/uwsgi/pull/19后,我明白使用uwsgi_modifier1 30;已被弃用.
所以这就是我解决问题的方法.
首先,我删除了这两行,删除了nginx中的SCRIPT_NAME处理:
uwsgi_param SCRIPT_NAME /foo;
uwsgi_modifier1 30;
Run Code Online (Sandbox Code Playgroud)
生成的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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我重新启动了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
Run Code Online (Sandbox Code Playgroud)
现在,我得到了预期的输出.
debian:~# curl http://mybox:8080/foo/bar
PATH_INFO: '/bar'
SCRIPT_NAME: '/foo'
REQUEST_URL: '/foo/bar'
Run Code Online (Sandbox Code Playgroud)