# Real IP not working?

**URL:** <https://www.ruby-forum.com/t/real-ip-not-working/214519>\
**Category:** NGINX\
**Created:** [December 4, 2011, 4:12pm UTC](https://www.ruby-forum.com/t/real-ip-not-working/214519 "2011-12-04T16:12:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [December 4, 2011, 4:12pm UTC](https://www.ruby-forum.com/t/real-ip-not-working/214519/1 "2011-12-04T16:12:47Z")

</div>

Hi. Nginx 1.0.8 proxying to Apache 2. My version of PHP is 5.3.8, but  
that’s irrelevant I suppose.

Nginx is with Real IP module. I have the following in my conf file:

```auto
set_real_ip_from 192.168.1.0/24;
set_real_ip_from 192.168.2.1;
set_real_ip_from 127.0.0.1;
set_real_ip_from [..my server ip..];
real_ip_header X-Real-IP;
```

Apache is also with module RPAF. And there’s this bit in httpd.conf:

```auto
LoadModule rpaf_module /usr/lib64/httpd/modules/mod_rpaf-2.0.so
<IfModule mod_rpaf.c>
RPAFenable On
RPAFproxy_ips 0.0.0.0 127.0.0.1 [...my server IPs...]
RPAFsethostname On
RPAFheader X-Real-IP
</IfModule>
```

Yet, in Apache log, the client IPs are not shown. All the IPs in the log  
are my own server’s IP!

How can I make sure that Apache shows the end user’s IP?

Posted at Nginx Forum:

> **[Real IP not working?](https://forum.nginx.org/read.php?2,219581,219581#msg-219581)**
>
> Hi. Nginx 1.0.8 proxying to Apache 2. My version of PHP is 5.3.8, but that's irrelevant I suppose. Nginx is with Real IP module. I have the following in my conf file: \[code\] set\_real\_ip\_from 192.168.1.0/24; set\_real\_ip\_from 192.168.2.1;

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [December 4, 2011, 7:37pm UTC](https://www.ruby-forum.com/t/real-ip-not-working/214519/2 "2011-12-04T19:37:38Z")

</div>

> set\_real\_ip\_from [..my server ip..];  
> RPAFenable On  
> How can I make sure that Apache shows the end user’s IP?  
> You shouldn’t use ngx\_http\_realip\_module for issues like this.  
> Just set X-Real-IP header to $remote\_addr variable value  
> in your location section with ‘proxy\_pass’ directive  
> (or ‘fastcgi\_pass’ or whatever you use).

Like the following:

```
location / {

```

…  
proxy\_pass http://\<backend\_ip\>:\<backend\_port\>;  
proxy\_set\_header Host $host;

- 

```
  proxy_set_header X-Real-IP $remote_addr;

```

…  
}

Your mod\_rpaf config have already correctly set up to handle X-Real-IP  
header  
and consider it as the end user’s IP.

Also, you shouldn’t use the RPAFsethostname option turned ‘On’ unless  
you  
set X-Host header in the nginx config before passing request to backend.  
Just turn it ‘Off’.

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [December 4, 2011, 8:16pm UTC](https://www.ruby-forum.com/t/real-ip-not-working/214519/3 "2011-12-04T20:16:39Z")

</div>

## Alexander Kolesen Wrote:

…

> ```
> proxy_set_header Host
> 
> ```
> 
> Also, you shouldn’t use the RPAFsethostname option  
> turned ‘On’ unless you  
> set X-Host header in the nginx config before  
> passing request to backend. Just turn it ‘Off’.

Thanks Alexander. But this does not work.

I commented all the “set\_real\_ip\_from” in nginx config. Restarted nginx.

Now the “REMOTE\_ADDR” in my php code inside Apache is just my server’s  
IP. So the real IP is not being passed.

If I enable the “set\_real\_ip\_from” inside nginx config, then my php code  
sees the right REMOTE\_ADDR. But the log messages inside Apache are wrong  
even then…all from my own server IP.

Anyway, my nginx “proxy.inc” is this:

`
proxy_cache_bypass $http_pragma $http_authorization;
proxy_no_cache $http_pragma $http_authorization;
proxy_temp_file_write_size 512k;
proxy_pass_header Set-Cookie;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Referer $http_referer;
proxy_set_header Host $host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
`

Any ideas?

PS: I turned that RPAFsethostname off in apache. This seems harmless  
enough.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,219581,219594#msg-219594](http://forum.nginx.org/read.php?2,219581,219594#msg-219594)

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [December 4, 2011, 8:19pm UTC](https://www.ruby-forum.com/t/real-ip-not-working/214519/4 "2011-12-04T20:19:45Z")

</div>

## pk899 Wrote:

> > ```
> > location / {
> > }
> > 
> > ```
> > 
> > passing request to backend. Just turn it ‘Off’.  
> > is just my server’s IP. So the real IP is not
> 
> proxy\_set\_header Referer  
> proxy\_set\_header X-Forwarded-For  
> $proxy\_add\_x\_forwarded\_for;
> 
> Any ideas?
> 
> PS: I turned that RPAFsethostname off in apache.  
> This seems harmless enough.

Btw, just to add…

The HTTP\_X\_FORWARDED\_FOR variable does have the right IP address, but  
the “REMOTE\_ADDR” does not. The latter has only my own server IP.

What do I need to do?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,219581,219595#msg-219595](http://forum.nginx.org/read.php?2,219581,219595#msg-219595)

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [December 5, 2011, 7:34am UTC](https://www.ruby-forum.com/t/real-ip-not-working/214519/5 "2011-12-05T07:34:07Z")

</div>

> > …  
> > to handle X-Real-IP header  
> > Thanks Alexander. But this does not work.
> 
> proxy\_set\_header Accept-Encoding ‘’;  
> Any ideas?
> 
> PS: I turned that RPAFsethostname off in apache. This seems harmless  
> enough.

Seems like you’re using Apache httpd \>=2.0, but your checks  
for the 1.3 version.  
Just try this:

LoadModule rpaf\_module /usr/lib64/httpd/modules/mod\_rpaf-2.0.so  
+  
-  
RPAFenable On  
RPAFproxy\_ips 0.0.0.0 127.0.0.1 […my server IPs…]  
RPAFsethostname On  
RPAFheader X-Real-IP

---

<div class="post-metadata">

**Author:** ![pk899](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@pk899](https://www.ruby-forum.com/u/pk899)\
**Post date:** [December 4, 2011, 9:09pm UTC](https://www.ruby-forum.com/t/real-ip-not-working/214519/6 "2011-12-04T21:09:13Z")

</div>

I’ve got nginx proxying to a lighttpd server (hosted on a DroboPro FS),  
and I’m using only the following four directives in the proxy location’s  
stanza:

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

This seems to be all that’s necessary. The proxy target’s access log  
shows the accessing client’s IP for each request, not the nginx server.

You’ve got a lot more stuff going on in your config, though, so I’m not  
sure if this will be helpful to you or not. Maybe you could comment out  
some directives and start with just the basics?

-Lee
