Nginx proxy to Apache - access remote host IP address using mod_praf
These days it i getting more and more common for people to use a lightweight webserver such as lighttpd or nginx as a frontend, and proxy to an Apache backend if they really need to.
There is however a small problem when using Apache as a backend, as it will see all requests as originating from localhost (127.0.0.1). So, if you need access to the users real IP address in your application, you will need to install the mod_praf apache module and configure Apache to use the and X-Real-IP headers that gets sent by nginx.
First, set up a name based apache virtual host listening on port 8080:
NameVirtualHost *:8080
<VirtualHost *:8080>
ServerName mydomain.com
ServerAdmin me@mydomain.com
DirectoryIndex index.php container.php index.html
DocumentRoot /path/to/web/root/
<Directory /path/to/web/root/>
Options FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Tell nginx to listen on port 80 and proxy all requests to Apache:
server {
listen 80;
server_name mydomain.com;
location / {
access_log off;
proxy_pass http://mydomain.com:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Install mod_praf:
sudo aptitude install libapache2-mod-rpaf
Then configure mod_praf by adding the following to your Apache virtual host:
RPAFenable On RPAFsethostname On RPAFproxy_ips 127.0.0.1
Restart apache:
sudo apache2ctl restart
And that's it. Apache should now have access to the original host IP address. Check your access logs for confirmation.
TODAY'S MOST POPULAR
-
4th Sep 08
-
8th Aug 08
-
7th Dec 08
MOST POPULAR ARTICLES
RECENT COMMENTS
-
2 hours 13 min ago
-
3 weeks 22 hours ago
-
4 weeks 1 day ago
-
4 weeks 6 days ago
-
6 weeks 6 days ago

Comments
1st Feb 2009, 7:07am
Does RPAF work with old Apache (1.3.34)?
30th Mar 2009, 10:22pm
the mod_rpaf distro has source for both apache1 and apache2.
Please share your thoughts, comments and suggestions...