通过 uwsgi_pass -> uwsgi -> flask 重定向到 python 代码后传递 nginx 变量的奇怪问题。uWSGI 本身会看到像 $uri 或 $request_filename 这样的变量被扩展。我的 python 代码将它们视为“$uri”和“$request_filename”
nginx配置:
location /l0 {
include uwsgi_params;
uwsgi_pass 127.0.0.1:4001;
}
location /spool {
set $file_path $request_filename;
post_action /l2;
}
location /l2 {
internal;
include uwsgi_params;
uwsgi_param REQUEST_URI /foo/bar=$file_path;
uwsgi_pass 127.0.0.1:4001;
limit_except GET {
allow all;
}
}
uwsgi 应用开始于:
/usr/bin/uwsgi_python --socket :4001 --python-path /my/path --module my.module --callable app --need-app
我从日志中看到 uwsgi 得到了 $uri 扩展的请求(即“/some/path”)。在我的代码中,我只看到“$uri”(见日志行my_2nd_handler got mypath=/foo/bar=$uri
)
代码:
app = Flask('myapp')
@app.route('/l0', methods=['GET']):
def my_root_handler():
r = Response()
r.headers['X-Accel-Redirect'] = '/spool/file1'
print "my_root_handler sent nginx to /spool/file1"
return r
@app.route('/<path:mypath>', methods=['GET']):
def my_2nd_handler(mypath):
print "my_2nd_handler got mypath="+mypath
要求:
curl -X GET http://mysite/l0
日志:
my_root_handler sent nginx to /spool/file1
my_2nd_handler got mypath=/foo/bar=$uri
[pid: 21715|app: 0|req: 1/1] 10.16.183.15 () {40 vars in 915 bytes} [Tue Jan 22 21:04:59 2013] GET /foo/bar=/spool/file1 => generated 116 bytes in 3 msecs (HTTP/1.1 404) 4 headers in 203 bytes (1 switches on core 0)
$request_filename 不是唯一的问题变量。我已经尝试了其他几个相同的结果。如何在 python 代码中扩展 nginx 变量?