# Nginx + apache : REMOTE\_ADDR and REMOTE\_HOST bug

**URL:** <https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244>\
**Category:** NGINX\
**Created:** [August 30, 2011, 6:07pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244 "2011-08-30T18:07:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 30, 2011, 6:07pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/1 "2011-08-30T18:07:19Z")

</div>

Hello,  
i’ve got a “funny” bug.

My installation is Nginx 1.0 and apache ( httpd ) behind.

Apache got’s mod\_rpaf activated and HostNameLookups On.

When testing a php script i find this problem :

\_SERVER[“REMOTE\_HOST”]\*\*\*\*\*\*\*\*\*\*\*\*\<= my webserver hostname  
\_SERVER[“REMOTE\_ADDR”]82.125.151.3\<= my ip

Any idea ??? it’s really strange.

Thank you

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 6:20pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/2 "2011-08-30T18:20:52Z")

</div>

Hello!

On Tue, Aug 30, 2011 at 06:06:37PM +0200, Albuquerque R. wrote:

> \_SERVER[“REMOTE\_ADDR”]82.125.151.3\<= my ip  
> I.e. client’s ip is correct, but client’s hostname still points to  
> your webserver, right? This looks like problem in mod\_rpaf.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 6:23pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/3 "2011-08-30T18:23:57Z")

</div>

Yes that’s right. It’s really strange that i’m the only one having this  
problem with mod\_rpaf 0.6 ☹ ☹ ☹

2011/8/30 Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 6:35pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/4 "2011-08-30T18:35:41Z")

</div>

BTW it’s not good idea to use HostNameLookups on production site since  
these lookups may take long enough time.

–  
Igor S.

30.08.2011, 20:23, Albuquerque R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ():

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 6:44pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/5 "2011-08-30T18:44:21Z")

</div>

Tha’s right but i really need this info… ☹

2011/8/30 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 6:48pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/6 "2011-08-30T18:48:04Z")

</div>

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

And on apache :

LoadModule rpaf\_module modules/mod\_rpaf-2.0.so  
RPAFenable On  
RPAFsethostname On  
RPAFproxy\_ips 127.0.0.1  
RPAFheader X-Forwarded-For

2011/8/30 Jim O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 6:44pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/7 "2011-08-30T18:44:20Z")

</div>

On 8/30/11 12:23 PM, Albuquerque R. wrote:

> ```
> > Hello,
> 
> I.e. client's ip is correct, but client's hostname still points to
> your webserver, right? This looks like problem in mod_rpaf.
> 
> ```

It looks like it’s reporting _his_ IP, not that of the client and Apache  
is doing a look up on that IP?

How are you passing the remote IP from nginx to Apache?

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 7:38pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/8 "2011-08-30T19:38:41Z")

</div>

On 8/30/11 12:47 PM, Albuquerque R. wrote:

> RPAFenable On  
> RPAFsethostname On  
> RPAFproxy\_ips 127.0.0.1  
> RPAFheader X-Forwarded-For

Please do not top post. It makes following a thread much more difficult.

We use the following in Apache:

RPAFenable On  
RPAFsethostname On  
RPAFproxy\_ips 127.0.0.1

First thing I would suggest is running a phpinfo script and seeing what  
the value of \_SERVER[“REMOTE\_ADDR”] is.

Then comment out the line that begins with “RPAF header”, restart Apache  
and run again and see if the value changes.

> ```
> this
> >
> > > _SERVER["REMOTE_ADDR"]82.125.151.3<= my ip
> >
> >
> nginx mailing list
> removed_email_address@domain.invalid <mailto:removed_email_address@domain.invalid>
> http://mailman.nginx.org/mailman/listinfo/nginx
> 
> ```
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 8:30pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/9 "2011-08-30T20:30:34Z")

</div>

2011/8/30 Jim O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > LoadModule rpaf\_module modules/mod\_rpaf-2.0.so [http://mod\_rpaf-2.0.so](http://mod_rpaf-2.0.so)  
> > RPAFenable On  
> > RPAFsethostname On  
> > RPAFproxy\_ips 127.0.0.1
> 
> First thing I would suggest is running a phpinfo script and seeing what  
> the value of \_SERVER[“REMOTE\_ADDR”] is.

> > Changes done, nothing changed.. Still the same problem…The fact is that  
> > it seems mod\_rpaf is working well because REMOTE\_ADDR is good, but  
> > REMOTE\_HOST is not good ☹ ☹ ☹

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 10:18pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/10 "2011-08-30T22:18:42Z")

</div>

On 8/30/11 2:29 PM, Albuquerque R. wrote:

> ```
> >
> 
> ```
> 
> > > Changes done, nothing changed.. Still the same problem…The fact is  
> > > that it seems mod\_rpaf is working well because REMOTE\_ADDR is good, but  
> > > REMOTE\_HOST is not good ☹ ☹ ☹

I agree. It is not resolving the IP incorrectly. It is not an issue with  
nginx or with mod\_rpaf.

> ```
> >
> > >
> Rui wrote:
> > > > _SERVER["REMOTE_HOST"] ************ <= my webserver hostname
> > How are you passing the remote IP from nginx to Apache?
> <mailto:removed_email_address@domain.invalid <mailto:removed_email_address@domain.invalid>>>
> >
> >
> 
> ```
> 
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 31, 2011, 1:15am UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/11 "2011-08-31T01:15:52Z")

</div>

On 8/30/11 5:50 PM, Maxim D. wrote:

> > > REMOTE\_HOST is not good ☹ ☹ ☹  
> > > … this is obviously a problem in mod\_rpaf. It fails to change  
> > > ip _before_ resolving happens in Apache. (No idea if it’s actually  
> > > possible to implement this correctly though.)

Maxim, we could argue this pedantically all night long and never agree.  
However, mod\_rpaf is doing exactly what it’s designed to do which is to  
report the remote IP address correctly. HostNameLookups is _not_ a  
function of mod\_rpaf, it is a function of Apache core.

However… the function works properly in _my_ testing as follows.

I created a small php script as follows:

\<?php print ('Your IP is "'.$\_SERVER['REMOTE\_ADDR'].'". '); print ('Your Host is "'.$\_SERVER["REMOTE\_HOST"].'".'); ?\>

I turned on “HostNameLookups” in Apache.

Attached is the result.

This is indeed my IP and host.

Why it’s not working for the OP I cannot explain, but it does seem to  
work _correctly_ as does “HostNameLookups” directive.

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 31, 2011, 8:35am UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/12 "2011-08-31T08:35:57Z")

</div>

2011/8/31 Jim O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > > that it seems mod\_rpaf is working well because REMOTE\_ADDR is good, but  
> > > > \> \> your webserver, right? This looks like problem in
> 
> ?\>
> 
> > While accessing the page directly from apache i’ve got the good values  
> > too. But when i pass by nginx then apache ( mod\_rpaf ) the hostname is  
> > not  
> > right ☹

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 30, 2011, 11:50pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/13 "2011-08-30T23:50:38Z")

</div>

Hello!

On Tue, Aug 30, 2011 at 04:17:52PM -0400, Jim O. wrote:

> On 8/30/11 2:29 PM, Albuquerque R. wrote:

[…]

> > > > Changes done, nothing changed.. Still the same problem…The fact is  
> > > > that it seems mod\_rpaf is working well because REMOTE\_ADDR is good, but  
> > > > REMOTE\_HOST is not good ☹ ☹ ☹
> 
> I agree. It is not resolving the IP incorrectly. It is not an issue with  
> nginx or with mod\_rpaf.

As I wrote in my original reply…

> > ```
> > > > I.e. client's ip is correct, but client's hostname still 
> > 
> > ```

points to

> > ```
> > > > your webserver, right? This looks like problem in mod_rpaf.
> > 
> > ```

… this is obviously a problem in mod\_rpaf. It fails to change  
ip _before_ resolving happens in Apache. (No idea if it’s actually  
possible to implement this correctly though.)

Maxim D.

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 31, 2011, 9:03am UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/14 "2011-08-31T09:03:46Z")

</div>

2011/8/31 Albuquerque R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > > nginx or with mod\_rpaf.  
> > > > possible to implement this correctly though.)
> > 
> > \<?php

I’ve patched the thing with a php script. In fact i don’t really need  
hostname everywhere and hostnamelookups is activated only for some  
vhosts.  
So this is the patch :

> <https://pastebin.com/4HmX790g>

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 31, 2011, 12:59pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/15 "2011-08-31T12:59:55Z")

</div>

On 31.08.2011 10:03, Albuquerque R. wrote:

> While accessing the page directly from apache i’ve got the good  
> values too. But when i pass by nginx then apache ( mod\_rpaf ) the  
> hostname is not right ☹

in request from nginx to apache backend - you set correct  
client hostname in the X-Forwarded-Host or X-Host header?

RPAFsethostname on;

means what you must provide desirable client hostname:

## source of mod\_rpaf-0.6\mod\_rpaf-2.0.c

if (cfg-\>sethostname) {  
const char _hostvalue;  
if (hostvalue = apr\_table\_get(r-\>headers\_in, “X-Forwarded-Host”)) {  
/_ 2.0 proxy frontend or 1.3 =\> 1.3.25 proxy frontend _/  
apr\_table\_set(r-\>headers\_in, “Host”, apr\_pstrdup(r-\>pool,  
hostvalue));  
r-\>hostname = apr\_pstrdup(r-\>pool, hostvalue);  
ap\_update\_vhost\_from\_headers(r);  
} else if (hostvalue = apr\_table\_get(r-\>headers\_in, “X-Host”)) {  
/_ 1.3 proxy frontend with mod\_proxy\_add\_forward \*/  
apr\_table\_set(r-\>headers\_in, “Host”, apr\_pstrdup(r-\>pool,  
hostvalue));  
r-\>hostname = apr\_pstrdup(r-\>pool, hostvalue);  
ap\_update\_vhost\_from\_headers(r);  
}  
}

–  
Best regards,  
Gena

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 31, 2011, 1:58pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/16 "2011-08-31T13:58:35Z")

</div>

2011/8/31 Gena M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> RPAFsethostname on;  
> apr\_table\_set(r-\>headers\_in, “Host”, apr\_pstrdup(r-\>pool,  
> }

In fact event with RPAFsethostname on; commented, there’s a problem. In  
this  
case we are talking about client remote hostname…

---

<div class="post-metadata">

**Author:** ![Albuquerque\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Albuquerque\_R](https://www.ruby-forum.com/u/Albuquerque_R)\
**Post date:** [August 31, 2011, 2:46pm UTC](https://www.ruby-forum.com/t/nginx-apache-remote-addr-and-remote-host-bug/211244/17 "2011-08-31T14:46:09Z")

</div>

On 31.08.2011 14:57, Albuquerque R. wrote:

> While accessing the page directly from apache i’ve got the good  
> values too. But when i pass by nginx then apache ( mod\_rpaf ) the  
> hostname is not right ☹

[…]

> In this case we are talking about client remote hostname…

probably this is bug / misfeature in mod\_rpaf / apache+mod\_rpaf.  
and obviously such behavior of apache+mod\_rpaf is not nginx bug.

mod\_rpaf change only client IP and not change client hostname  
see details in fuction “change\_remote\_ip” in mod\_rpaf sources

p.s. most probably this is mod\_rpaf bug / misfeature.  
may be try ask support in mod\_rpaf/apache mail lists?

–  
Best regards,  
Gena
