Jump to Navigation

Nginx proxy to Apache - access remote host IP address using mod_praf

Image to accompany story

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.

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.

Comments

Vladimir's picture

Does RPAF work with old Apache (1.3.34)?

Anonymous's picture

the mod_rpaf distro has source for both apache1 and apache2.

Please share your thoughts, comments and suggestions...

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
If you have your own website, enter its address here and we will link to it for you. (please include http://).
eg. http://www.kirkdesigns.co.uk