How do I pass the real IP of Gunicorn when using Nginx and CloudFlare?
I have the following nginx configuration:
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;
}
}
However, in Django, the following code returns nothing to me:
request.META.get('REMOTE_ADDR', '') # this gives me b''
How can I fill in the REMOTE_ADDR
user's real IP address?
Note. When I remove the #Cloudflare section of the nginx config, it is REMOTE_ADDR
populated with the Cloudflare IP.
Refresh . I added the following to my nginx config, however it request.META.get('REMOTE_ADDR')
is still empty.
location / {
...
proxy_set_header REMOTE_ADDR $proxy_add_x_forwarded_for;
}
+3
source to share