1

I'm not new to apache, but I'm pulling my hair out on this one.

I can't get a 2nd VirtualHost up on a particular IP. It seems to just use the default VirtualHost for that IP.

I have a server which owns two IP addresses. It runs on ports 80 and 443 on both IPs, such that I have four NameVirtualHosts e.g.

NameVirtualHost 1.1.1.1:80
NameVirtualHost 1.1.1.1:443
NameVirtualHost 2.2.2.2:80
NameVirtualHost 2.2.2.2:443

Now I have a default (i.e. file is named such that it would be sourced before the next one) VirtualHost for 2.2.2.2:80 defined as

<VirtualHost 2.2.2.2:80>
    ServerAdmin [email protected]
    CustomLog /var/log/apache2/ip2-80-access.log combined 
    ErrorLog /var/log/apache2/ip2-80-error.log
</VirtualHost>

And a 2nd one

<VirtualHost 2.2.2.2:80>
    ServerAdmin [email protected]
    ServerName foo.example.com
    DocumentRoot /var/www/foo.example.com
    CustomLog /var/log/apache2/ip2-80-access.log combined 
    ErrorLog /var/log/apache2/ip2-80-error.log
</VirtualHost>

DNS is set up so that foo.example.com points to 2.2.2.2.

But when I try to access foo.example.com/anything I get a 404 not found. It seems apache is ignoring my 2nd name-based and ip-based virtualhost.

I'm using Apache 2.2 on Debian 6 (Squeeze). Please note: I am not trying to get apache to choose a virtual host for SSL here (I am aware of limitations/SNI etc.), this is standard port 80.

1 Answer 1

2

Use apache2ctl -S to list the virtualhosts and defaults.

This shows that apache is selecting default VirtualHost for this IP:Port as the one for the specified host name:

VirtualHost configuration:
2.2.2.2:80     is a NameVirtualHost
         default server foo.example.org.uk (/etc/apache2/sites-enabled/2.2.2.2:1)
         port 80 namevhost foo.example.org.uk (/etc/apache2/sites-enabled/2.2.2.2:1)
         port 80 namevhost foo.example.org.uk (/etc/apache2/sites-enabled/2.2.2.2:18)
2.2.2.2:443    is a NameVirtualHost
         ...
1.1.1.1:80     is a NameVirtualHost
         ...etc...

i.e. I had thought that because I was supplying a host name that matched a VirtualHost's ServerName directive, and not supplying a ServerName in the default, that the 2nd one would be chosen.

However, what Apache seems to do is use the hostname for 2.2.2.2 from /etc/hosts as the hostname for any requests on that IP. This is a bit opaque IMO.

So 2 alternative ways to get it working:

  1. change the hostname in /etc/hosts e.g. to foo2.example.org.uk. Restart Apache and now requests for foo.example.org.uk/something work. Of course, this is really proof of concept and you probably want your server name to match the main server name.

  2. add a ServerName directive to the first VirtualHost. It can be anything, e.g. goaway.local. This will mean that requests on the proper domain name get served by the correct VirtualHost block an that anything else (e.g. all those annoying direct IP vulnerability probes) are handled by the default, which can be set to deny everything.

2
  • +1 for the useful apachectl command (didn't know about that). However, I'm curious what is the point of that first/unused VirtualHost if you are not going to use it? Side note: I always setup my web/app servers as if it was a member in a cluster, so their hostnames will never match that of the site URL (so it would have been covered by alt 1).
    – martian111
    Jul 9, 2013 at 21:05
  • Thanks. I was of the mindset at the time that it was cleaner/safer/easier to maintain the default as a No-no-go-away, and then to have the more general stuff in a nice clean separate virtual host. Actually I abandoned that and do now use the default one with a RewriteRule to check that the person acceseed it via the correct domain. Jul 10, 2013 at 15:14

You must log in to answer this question.

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