您没有转发代理IP。这是我在Nginx配置中设置的一组转发标头:
location / {
proxy_set_header Host $http_host;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass ......;
}
Nginx文档中的更多选项-http://nginx.org/zh/docs/http/ngx_http_proxy_module.html#proxy_set_header
然后在Django中,您可以执行以下操作:
user_ip = request.Meta['HTTP_X_REAL_IP`] or request.Meta['REMOTE_ADDR']
请注意,X-Forwarded-Proto
在将Django与SSL结合使用时,这是必要的,在这种情况下,您还需要对Django进行一些配置:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
Django文档中的更多内容-https://docs.djangoproject.com/zh/1.9/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER