我设置了 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/