python - 如何读取从nginx传递的python/flask中的UWSGI参数

标签 python nginx flask uwsgi

我设置了 python/flask/uwsgi+nginx 网络应用程序,它工作正常。我想使用 geoip,我在 nginx 端设置了它:

   location / {
            include         uwsgi_params;
            uwsgi_pass      unix:/tmp/qbaka-visit.sock;
            ...
            uwsgi_param     GEOIP_COUNTRY_CODE $geoip_country_code;
    }

但是现在我不知道如何在 python 中读取这个属性。在 uwsgi 之前,我使用了简单的 flask 内置网络服务器 + nginx proxy_pass,在这种情况下,我使用了 proxy_set_header X-Geo-Country $geoip_country_code; 并使用 request.headers 读取这个参数,但是对于 UWSGI 参数,我不知道如何读取它们。

最佳答案

uwsgi_param 为应用程序设置给定名称的 wsgi environ 键。您可以将其用于 header , header 遵循使用 HTTP_ 前缀的 CGI 约定。你的 proxy_set_header 的等价物是:

uwsgi_param HTTP_X_GEOIP_COUNTRY $geoip_country_code;

请注意, header 名称必须为大写,并且破折号由下划线代替,以便在 wsgi 中被识别为有效 header 。

或者,看起来环境可以在 flask 中访问,如 request.environ,所以你可以保持你的 uwsgi_param 相同,但将其读取为 request.environ['GEOIP_COUNTRY_CODE' ]。实际上,这可能更可取,因为您可以通过这种方式将它们与实际请求 header 区分开来。

关于python - 如何读取从nginx传递的python/flask中的UWSGI参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14133460/

相关文章:

python - 如何创建一个迭代器来生成项目,其中没有项目的单个字符在 python 中表示超过 n 次?

python - Mongoengine PointField 给出预期的位置对象,位置数组格式不正确错误

spring - 如何在使用 Nginx 作为反向代理的 Spring Boot 应用程序上记录嵌入式 Tomcat 访问日志上的真实客户端 IP?

python - 为什么返回的 JSON 被理解为 unicode 而不是列表?

jquery - jsonify 字典列表并作为 ajax 请求的响应发送

python - Tensorflow 对象检测示例 : TypeError: '<' not supported between instances of 'tuple' and 'str'

python - 单击按钮后更改模型字段值

http - NGINX proxy_pass 变更方案

ruby - nginx 背后的 Unicorn vs Passenger Standalone

python-3.x - Jsonify 异常错误