title: How to make nginx behind reverse proxy allow access based on IP address from different header
body: <p>I am trying to allow access to specific location of my website only for specific IP address.</p>
  
  <p>I have two servers: load balancer with nginx and app server with nginx as well.
  I am trying to allow requests to some location based on client's IP address.
  For debugging, I added logging of headers to application and here is what I see there (i.e. I guess reverse proxy correctly transfers real client IP address):</p>
  
  <pre><code>"REMOTE_ADDR": "load.balancer.ip.address",
  "HTTP_X_REAL_IP": "real.client.ip.address",
  </code></pre>
  
  <p>So I added location directive to nginx as follows:</p>
  
  <pre><code>location /upload  {
          allow real.client.ip.address;
          deny  all;
          alias /path/to/upload/folder;
  }
  </code></pre>
  
  <p>Now when I try to access location <code>/upload/something</code> in browser, I see error 403 by nginx.
  But when I remove <code>deny all</code> directive or change <code>real.client.ip.address</code> to <code>load.balancer.ip.address</code> I am able to access the location successfully.</p>
  
  <p>How can I force nginx to check client's IP address in different header when it decides to <code>allow</code> or <code>deny</code>?</p>