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 02 '18 at 23:57
  • Apache 2.2. Thanks for the suggestion! – Chris Tanner Apr 03 '18 at 02: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 03 '18 at 02: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 03 '18 at 03:37
  • Have a look at http://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 03 '18 at 04:06
  • @PatrickMevzek you are correct, sir. Thanks for that. If you can make your solution an answer I'll accept it. – Chris Tanner Apr 03 '18 at 04:26

2 Answers2

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

Patrick Mevzek
  • 9,921
  • 7
  • 32
  • 43
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>
Ben M.
  • 171
  • 5
  • Thanks, Ben. Is there an alternative to moving the entire docroot? `domain1` is a live prod server. Is there any explanation for why the first vhost is matching when a different domain is requested? – Chris Tanner Apr 03 '18 at 03:33
  • @ChrisTanner : have a look at http://httpd.apache.org/docs/2.2/vhosts/name-based.html#using – Patrick Mevzek Apr 03 '18 at 04:09