title: X-Real-IP header confusion in nginx
body: <p>My nginx config is - </p>
  
  <pre><code>    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
      set_real_ip_from 55.55.55.1;
      real_ip_header X-Forwarded-For;
      real_ip_recursive on;
  </code></pre>
  
  <p><br/></p>
  
  <p>I would like to know if I understand this config correctly.
  Let us say a user has a machine - <br/><br/>
  1. At ip <code>11.11.11.11</code> <br/>
  2. The user is behind a proxy which has an IP <code>22.22.22.22</code> <br/>
  3. User sends a request, which reaches the load balancer at <code>55.55.55.1</code>, which routes it to nginx.</p>
  
  <p>Then, once nginx has done its work, the request will have following headers reaching the server - <br/>
  a. <code>X-Forwarded-For: [11.11.11.11, 22.22.22.22, 55.55.55.1]</code> <br/>
  b. <code>X-Real-IP: 22.22.22.22</code> (since nginx will recurse on <code>X-Forwarded-For</code> from end to array to start of array, and find the first untrusted ip) <br/>
  c. If <code>X-Forwarded-For</code> does not exist in a request, then <code>$remote_addr</code> value is used in <code>X-Real-IP</code> header, otherwise it is over-written by recursing on <code>X-Forwarded-For</code> header array, taking into consideration <code>set_real_ip_from</code> rule(s). </p>
  
  <p>Please let me know if points 'a', 'b' and 'c' are correct or incorrect. If incorrect, I would like to know the reason why?</p>