Nginx Reverse Proxy – Routing to Apache in a Multisite Setup

apache-2.2nginxPROXYreverse-proxy

I'm running an Ubuntu 10.04 nginx server, with a reverse proxy to apache (to run munin monitoring).

This is an excerpt of my default Apache site file:

<VirtualHost *:8090>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/cache/munin/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

And an excerpt of my example.com nginx configuration file:

location /stats {
    proxy_pass              http://127.0.0.1:8090/;
    proxy_redirect          off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffers           32 4k;
}

Whenever I go to example.com/stats, nginx point to apache over port 8090, and apache serves up the munin web directory. It works great.

But what if I wanted to add another domain, say example.org? I'd have another nginx configuration file, but how would I make apache's DocumentRoot something else? Since the requests are coming into apache from nginx through localhost port 8090, how can apache determine which site the request is coming from, and which DocumentRoot/configuration to serve it with?

I'm assuming I will have to use the headers set by nginx ($host; or $proxy_add_x_forwarded_for; variables?) in apache somehow…

Best Answer

I run several domains on one nginx.conf file. Try this:

server {
    listen xxx.xxx.xxx.xxx:8090;
    server_name example.org;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://127.0.0.1:8090;
    }
}

server {
    listen xxx.xxx.xxx.xxx:8090;
    server_name example.com;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://127.0.0.1:8090;
    }
}

then add have two virtual configs in your apache

<VirtualHost *:8090>
ServerName example.org
ServerAdmin webmaster@localhost

DocumentRoot /var/cache/munin/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
</VirtualHost>

<VirtualHost *:8090>
ServerName example.com
ServerAdmin webmaster@localhost

DocumentRoot /var/cache/munin/www2
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
</VirtualHost>