# Confused about X-Forwarded-For

**URL:** <https://www.ruby-forum.com/t/confused-about-x-forwarded-for/135965>\
**Category:** NGINX\
**Created:** [April 27, 2008, 6:18am UTC](https://www.ruby-forum.com/t/confused-about-x-forwarded-for/135965 "2008-04-27T06:18:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rt\_Ibmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@Rt\_Ibmer](https://www.ruby-forum.com/u/Rt_Ibmer)\
**Post date:** [April 27, 2008, 6:18am UTC](https://www.ruby-forum.com/t/confused-about-x-forwarded-for/135965/1 "2008-04-27T06:18:32Z")

</div>

I am using nginx as a load balancing proxy to pass requests using  
proxy\_pass to upstream servers running my web app.

These requests may or may not come in with X-Forwarded-For, depending on  
whether the visitor is using a proxy.

On those upstream servers, within my web app, I need to get the IP  
address of the client browser.

Without nginx in the picture, my logic would be as follows:

- if X-Forwarded-For is set in the header, get the FIRST IP address in  
the list from X-Forwarded-For
- if there is no X-Forwarded-For in the header, use the REMOTE\_ADDR  
environment variable.

Now once I put nginx into the mix in front of the upstream servers, this  
is where I am getting confused. I have seen examples that show this:

proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-Real-IP $remote\_addr;

HOWEVER, I think I would then have to change my logic to look for  
X-Forwarded-For, and if not found, look for X-Real-IP.

What I would like to do instead is to just to have nginx add the real IP  
($remote\_addr) to the X-Forwarded-For header. This way my app can just  
always look for the first entry in the X-Forwarded-For list of IPs to  
get the IP I need.

So, I would like to know if the following would work to do this trick:

proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-Forwarded-For $remote\_addr;

Would that give me the result I am looking for by putting the real IP in  
the X-Forwarded-For header? And if that header is present, would it add  
it to the list or clobber previous entries? Perhaps instead it needs to  
be written instead as:  
proxy\_set\_header X-Forwarded-For  
$proxy\_add\_x\_forwarded\_for,$remote\_addr;

but in the event there was nothing in the X-Forwarded-For header it  
would wind up giving me a: “X-Forwarded-For ,x.y.z.a” and that leading  
comma would mess me up.

Ideas? Thank you!

Thanks!

```
  ____________________________________________________________________________________

```

Be a better friend, newshound, and  
know-it-all with Yahoo! Mobile. Try it now.  
[http://mobile.yahoo.com/;\_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ](http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ)
