【问题标题】:uwsgi flask application got nginx variables unexpandeduwsgi 烧瓶应用程序未扩展 nginx 变量
【发布时间】:2013-01-23 05:43:36
【问题描述】:

通过 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 变量?

【问题讨论】:

  • 像这样改变参数,uwsgi_param key $request_uri;工作吗?
  • 你的意思是 uwsgi_param 键中没有美元符号吗?如果是这样就已经是这样了uwsgi_param REQUEST_URI /foo/bar=$file_path;

标签: python nginx flask uwsgi


【解决方案1】:

很遗憾,我没有找到问题的根源。对我来说,它看起来像是 HttpUwsgiModule 中的一个错误。我没时间挖。所以我使用了解决方法:我通过uwsgi --http-socket 运行应用程序的另一个实例并使用proxy_pass 而不是uwsgi_pass。用作魅力。

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2012-11-28
    • 1970-01-01
    • 2019-04-05
    • 2021-01-27
    • 2015-09-18
    • 1970-01-01
    • 2018-08-21
    • 2015-07-18
    相关资源
    最近更新 更多