Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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.

share|improve this answer
2  
This will only work with NameVirtualHost * instead of NameVirtualHost *:80 –  Marcel Apr 9 '13 at 3: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 9 '13 at 3:42
    
so this answer I gave is wrong, please correct me there too. –  Marcel Apr 9 '13 at 3:52

Change NameVirtualHost *:80 to NameVirtualHost *

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

share|improve this answer
    
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 9 '13 at 2:31
    
change <VirtualHost _default_:80> to <VirtualHost ip.ip.ip.ip:80>. Also, I'm assuming apache 2.4, because you did not explicitly stated. –  Marcel Apr 9 '13 at 2:35
1  
try changing _default_:80 to *:80 in your first virtualhost definition –  Marcel Apr 9 '13 at 3:19
    
Please explain to me why this will make a difference. Thanks. –  tedneigerux Apr 9 '13 at 3: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 9 '13 at 3:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.