How to fix this virtual host setup?

2019-08-04 23:28发布

问题:

I have setup up 2 virtual hosts that share the same IP on a centos server running apache 2.

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

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

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

Regardless of what URL I point to. Site A shows up.

How can I fix? I should have a.com going to a and b.com going to b.

Thanks all

回答1:

Is the following set in your httpd.conf?

NameVirtualHost *

An example from the documentation:

NameVirtualHost *

<VirtualHost *>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>


回答2:

make sure you have this directive as well

NameVirtualHost *

it could be displaying site A because it's set as the default document root. check your global settings, make sure the document root is not set to the same root as site A.



回答3:

I have always declared the domain as the name.. never had issue.

NameVirtualHost localhost:80

<VirtualHost localhost:80>
DocumentRoot "/home/eddie/workspace/"
</VirtualHost>

<VirtualHost digbiz.localhost:80>
DocumentRoot "/home/eddie/workspace/Digital_Business/app/webroot"
</VirtualHost>

<VirtualHost wishlist.localhost:80>
DocumentRoot "/home/eddie/workspace/WishList/app/webroot"
</VirtualHost>

<VirtualHost phpmyadmin.localhost:80>
DocumentRoot "/srv/www/phpMyAdmin"
</VirtualHost>


<VirtualHost test.localhost:80>
DocumentRoot "/home/eddie/workspace/CakePHPTesting/app/webroot"
</VirtualHost>

<VirtualHost auth.localhost:80>
DocumentRoot "/home/eddie/workspace/EntMeetCapt/app/webroot"
</VirtualHost>