title: How do I pass real IP to Gunicorn when using Nginx and CloudFlare?
body: <p>I have the following nginx configuration:</p>
  
  <pre><code>http {
  
      ... &lt;content removed for bevity&gt; ...
  
      # 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;
  
          ... &lt;content removed for bevity&gt; ...
  
          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;
          }
  
      }
  </code></pre>
  
  <p>Yet in Django the following code returns nothing to me:</p>
  
  <pre><code>request.META.get('REMOTE_ADDR', '') # this gives me b''
  </code></pre>
  
  <p>How can I populate <code>REMOTE_ADDR</code> with the user's real IP address?</p>
  
  <p>Note: When I remove the #Cloudflare section of the nginx configuration, <code>REMOTE_ADDR</code> is populated with Cloudflare's IP Address.</p>
  
  <hr>
  
  <p><strong>Update</strong>: I've added the following to my nginx configuration, however <code>request.META.get('REMOTE_ADDR')</code> is still empty.</p>
  
  <pre><code>location / {
      ...
      proxy_set_header REMOTE_ADDR $proxy_add_x_forwarded_for;
  }
  </code></pre>