Question

OS: centOS 6.3 Final

I've installed the mysql and apache2(httpd) packages and changed the config in /etc/httpd/conf/httpd.conf as seen below:

<VirtualHost *:80>
    DocumentRoot /var/www/html/wordpress/
    ServerName www.asterix.int
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/html/staticSite/
    ServerName www.meins.lan
    ServerAlias www.deins.lan
    ServerAlias www.obelix.int
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /var/www/html/joomla/
    ServerName www.example.com
    ServerAlias www2.example.com
    ServerAlias www3.example.com
</VirtualHost>

All ServerName and ServerAlias are working fine and I can access the 3 pages.

But when I'm trying to access a page over the server's ip something strange (at least for me) happens. apache2 returns me the index page of the first defined virtualHost(in this example wordpress). I've tried this with all 3 virtualHosts and get the same Results.

Is this a normal behavior or what I'm doing false ?

If this is a normal behavior: Can I set the DocumentRoot exclusively for all requests to the ip ?

Thx !

Was it helpful?

Solution

This is the intended behavior. If you use the IP (let's say http://123.123.123.123/), Apache will use 123.123.123.123 as HTTP the hostname. Since there is no VirtualHost with a ServerName or ServerAlias of 123.123.123.123, the first VirtualHost is used.

So if you want a VirtualHost that listens only for http://123.123.123.123/ you can simply create a VirtualHost with:

<VirtualHost *:80>
    DocumentRoot /var/www/html/my-ip-site/
    ServerName 123.123.123.123
</VirtualHost>

OTHER TIPS

This is a normal behaviour. You are using a vhost-method called name-based virtual hosts. As you can imagine that means, that the "routing" of the apache is then only done by the hostname in the HTTP-request, according the ServerName and ServerAlias directives in the config.

As the request to the IP of your server - e.g. 1.2.3.4 - can not be routed into any of the defined vhost, apache takes the default virtualhost. The default virtualhost is more or less the first virtualhost defined. The request on the IP is accepted, because you used wildcard definitions *:80.

You can check the virtualhosts set in apache by the apache

# command apache2 -S
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top