0

What is wrong here?

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName domain1.com
    ServerAdmin membersupport@domain1.com
    DocumentRoot /var/www/html/domain1-com
</VirtualHost>

<VirtualHost *:80>
    ServerName domain2.com
    ServerAlias www.domain2.com
    ServerAdmin hello@domain2.com
    DocumentRoot /var/www/html/domain2-com
</VirtualHost>

Problem:

domain1.com correctly serves the content at /var/www/html/domain1-com, but domain2.com also serves the content at /var/www/html/domain1-com.

Here is the output of httpd -S

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server domain1.com (/etc/httpd/conf/extra/httpd-vhosts.conf:21)
         port 80 namevhost domain1.com (/etc/httpd/conf/extra/httpd-vhosts.conf:21)
         port 80 namevhost domain2.com (/etc/httpd/conf/extra/httpd-vhosts.conf:27)
Syntax OK

This is apache version 2.2.22.

Thanks in advance.

UPDATED - Based on the answers below, I updated my conf file and the question. Unforunately the problem still exists. (I had only added _default_ as part as my attempt to resolve the issue myself prior to asking the question.)

tedneigerux
  • 29
  • 1
  • 7

2 Answers2

1

When configuring name based virtualhosts the IP:PORT pattern you give to the NameVirtualHost directive must be exactly the same as the one you give to each of your virtual hosts.

So your config must be:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName domain1.com
    DocumentRoot /var/www/html/domain1-com
</VirtualHost>

<VirtualHost *:80>
    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /var/www/html/domain2-com
</VirtualHost>

You do not, nor should you, use _default_. The default IP pattern is used in IP based virtual host configurations, to indicate which is the host used when a request comes in to an IP where there is no virtual host configured. This is not what you need here. Mixing IP based and Name based virtual hosts is complicated, and can lead to unexpected results...

With name based virtual hosts there is also a default host, from which all request are served that do not match a servername in one of your virtual hosts blocks, but this is always the first one.

Krist van Besien
  • 1,862
  • 13
  • 16
  • 2
    This will only work with `NameVirtualHost *` instead of `NameVirtualHost *:80` – Marcel Apr 09 '13 at 03:26
  • @marcel: No. This is exactly how it is supposed to work, and this works on every server I've setup so far. Also have a look at the examples in the apache docs. – Krist van Besien Apr 09 '13 at 03:42
  • so [this answer](http://serverfault.com/questions/497042/virtualhost-wont-allow-two-domains/497075#497075) I gave is wrong, please correct me there too. – Marcel Apr 09 '13 at 03:52
0

Change NameVirtualHost *:80 to NameVirtualHost *

The thing is that with wildcard and port, apache will do a exact match on the virtualhost list.

Marcel
  • 1,730
  • 10
  • 15
  • Made the change suggested above. httpd -S: [Tue Apr 09 02:30:34 2013] [error] VirtualHost *:80 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results – tedneigerux Apr 09 '13 at 02:31
  • change `` to ``. Also, I'm assuming apache 2.4, because you did not explicitly stated. – Marcel Apr 09 '13 at 02:35
  • 1
    try changing `_default_:80` to `*:80` in your first virtualhost definition – Marcel Apr 09 '13 at 03:19
  • Please explain to me why this will make a difference. Thanks. – tedneigerux Apr 09 '13 at 03:20
  • If it works, then it would have make a difference. It's because of `_default_:80` apache is complaining of you using non-ports and *:port setup. Just try that. – Marcel Apr 09 '13 at 03:23
  • if you're really interested in knowing what `_default_` does, go [here](http://httpd.apache.org/docs/2.2/vhosts/examples.html#default) and find out for yourself. TL;DR is that `_default_` virtualhosts definitions handles requests not in any other vhosts, not what you want. – Marcel Apr 09 '13 at 03:31