如果是通过 flask 的
request.remote_addr
获取的 ip 都是127.0.0.1
解决方案:
nginx.conf中添加
1 | # proxy_set_header Host $host:80; |
全文
1 | server { |
Python代码
1 | if request.headers.getlist("X-Forwarded-For"): |
查自:V2EX
拓展:
X-Forwarded-For 可能会有多个 IP ,如果浏览器使用了代理的话
正确的做法应该是抓 X-Real-IP
,或者分隔 X-Forwarded-For
取第 1 个值
Author: Mrli
Link: https://nymrli.top/2019/03/02/flask-nginx如何获得真实IP/
Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.