0

Running CentOS 6, Apache.

I have the following in my httpd.conf:

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName domain1.example
    ServerAlias www.domain1.example
</VirtualHost>

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

Whenever I visit domain2.example (or www.domain2.example), I am shown domain1.example.

EDIT: More info from httpd.conf

Listen 80
#NameVirtualHost *:80

Things I have checked:

  • Apache has been restarted
  • DNS for domain1.example and domain2.example point to same IP

What am I missing?

SOLUTION:

As Patrick points out, in Apache 2.2, NameVirtualHost is required when using multiple VirtualHosts. The solution here was to uncomment the line in httpd.conf:

#NameVirtualHost *:80

to:

NameVirtualHost *:80
  • Is this apache 2.2 or 2.4? You might consider moving domain2.com DocumentRoot into it's own directory that is not in /var/www/html – JohnA Apr 2 '18 at 23:57
  • Apache 2.2. Thanks for the suggestion! – Chris Tanner Apr 3 '18 at 2:04
  • You are not showing everything. What is your NameVirtualHost and Listen directives? Does the DNS resolution works correctly from the server and the client querying it? – Patrick Mevzek Apr 3 '18 at 2:07
  • @PatrickMevzek added more info. NameVirtualHost is commented out. Is this necessary in this situation? Every example I've seen on the web doesn't specify this being on. – Chris Tanner Apr 3 '18 at 3:37
  • Have a look at httpd.apache.org/docs/2.4/mod/core.html#namevirtualhost : "Prior to 2.3.11, NameVirtualHost was required to instruct the server that a particular IP address and port combination was usable as a name-based virtual host. In 2.3.11 and later, any time an IP address and port combination is used in multiple virtual hosts, name-based virtual hosting is automatically enabled for that address." You are on 2.2 so NameVirtualHost is mandatory in your case... – Patrick Mevzek Apr 3 '18 at 4:06
1

As you are using only Apache 2.2 you absolutely need to use the NameVirtualHost directive in order to let Apache know that you have multiple websites under different names but with the same IP.

The directive became deprecated in Apache 2.3, as written in this documentation: httpd.apache.org/docs/2.4/mod/core.html#namevirtualhost

Prior to 2.3.11, NameVirtualHost was required to instruct the server that a particular IP address and port combination was usable as a name-based virtual host. In 2.3.11 and later, any time an IP address and port combination is used in multiple virtual hosts, name-based virtual hosting is automatically enabled for that address.

For your version, the guide on configuration name based virtual host is here: http://httpd.apache.org/docs/2.2/vhosts/name-based.html#using

0

Looks like it's matching to the first vhost.

Try something like this:

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

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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