【发布时间】:2014-10-05 18:53:50
【问题描述】:
我有以下 nginx 配置:
http {
... <content removed for bevity> ...
# Cloudflare
set_real_ip_from 199.27.128.0/21;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
real_ip_header CF-Connecting-IP;
upstream myproject {
server unix:/var/www/myproject/gunicorn.sock fail_timeout=0;
}
server {
listen XXX;
server_name XXXXXX;
... <content removed for bevity> ...
location / {
proxy_set_header HOST $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://myproject;
}
}
然而在 Django 中,以下代码对我没有任何回报:
request.META.get('REMOTE_ADDR', '') # this gives me b''
如何使用用户的真实 IP 地址填充 REMOTE_ADDR?
注意:当我删除 nginx 配置的#Cloudflare 部分时,REMOTE_ADDR 会填充 Cloudflare 的 IP 地址。
更新:我已将以下内容添加到我的 nginx 配置中,但 request.META.get('REMOTE_ADDR') 仍然为空。
location / {
...
proxy_set_header REMOTE_ADDR $proxy_add_x_forwarded_for;
}
【问题讨论】:
标签: django nginx gunicorn cloudflare