title: NGinx $proxy_add_x_forwarded_for and real_ip_header
body: <p>I have a webapp under NGinx and another frontal load balancer, something like below (x.x.x.x = IP address):  </p>
  
  <blockquote>
    <p>Client(a.a.a.a) -> LB (b.b.b.b) -> NGX (c.c.c.c) -> WEBAPP (d.d.d.d)</p>
  </blockquote>
  
  <p>Here is a snippet of my NGinx configuration:</p>
  
  <pre><code>location / {
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header  X-Real-IP       $remote_addr;
      real_ip_header    X-Forwarded-For;
      set_real_ip_from  b.b.b.b;
      real_ip_recursive on;
  }
  </code></pre>
  
  <ol>
  <li>The load balancer add <code>X-Forwarded-For</code> field with client IP<br>
  <code>X-Forwarded-For</code> = <code>a.a.a.a</code></li>
  <li>NGinx search for client real IP in <code>X-Forwarded-For</code> header by omiting LB IP (<code>b.b.b.b</code>) and change <code>$remote_addr</code> from <code>b.b.b.b</code> to <code>a.a.a.a</code> so <code>proxy_set_header X-Real-IP $remote_addr</code> become true (OK that's what I want !)<br>
  BUT, NGinx also complete <code>X-Forwarded-For</code> header with <code>a.a.a.a</code> IP instead of <code>b.b.b.b</code></li>
  <li>WEBAPP receive the following headers:<br>
  <code>X-Forwarded-For</code> = <code>a.a.a.a, a.a.a.a</code><br>
  <code>X-Real-IP</code> = <code>a.a.a.a</code><br>
  -> <code>X-Forwarded-For</code> should be <code>a.a.a.a, b.b.b.b</code>  </li>
  </ol>
  
  <p>What I need is the ability to set first <code>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for</code> and then search for real IP and replace <code>$remote_addr</code> value.</p>
  
  <p>Any one can help me to solve this problem ?</p>