3

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...

3
  • 1
    Nginx passes the headers through to the backend server - one of which is the Host header. Just add a ServerName directive to your Apache VirtualHost block, and create one block per domain and Apache will serve the correct VirtualHost (using the Host header it receives). You could test it by accessing the apache page directly with something like curl --header "Host: example.com" localhost:8090
    – cyberx86
    Jan 2, 2012 at 3:16
  • You can also run the next Apache's VirtalHost in another port, like you did with munin in 8090. Jan 2, 2012 at 3:49
  • Ahh, okay. So apache sees the same information about the host just as if it was alone, serving the files firsthand.
    – mr_schlomo
    Jan 2, 2012 at 15:00

1 Answer 1

4

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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .