Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Multiple VirtualDocumentRoot

Tags:

apache

apache2

Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?

This is naming convention I am currently using and would like to continue to use:

host                    directory
127.0.0.1 domain        domain.com
127.0.0.1 sub.domain    domain.com_sub

Then in my vhosts section of the httpd.conf I have:

NameVirtualHost 127.0.0.1

<VirtualHost 127.0.0.1>
    VirtualDocumentRoot /var/www/%0.0.com
</VirtualHost>

<VirtualHost 127.0.0.1>
    VirtualDocumentRoot /var/www/%2.0.com_%1
</VirtualHost>

The problem with this is when I visit sub.domain the Apache error log shows that it is looking for /var/www/sub.domain.com rather than /var/www/domain.com_test which leads me to believe it only reads the first rule and then fails, but what I would like it to do is use any document root that satisfies either of the two VirtualDocumentRoot rules.

like image 217
Ben Avatar asked Feb 15 '09 08:02

Ben


People also ask

What is a VirtualDocumentRoot?

VirtualDocumentRoot is a directive to the Apache module mod_vhost_alias. It sets the document root to a dynamic path that may contain variables which are evaluated when an actual request is handled.

What is mod_vhost_alias?

mod_vhost_alias. The VirtualDocumentRoot directive allows you to determine where Apache HTTP Server will find your documents based on the value of the server name. The result of expanding interpolated-directory is used as the root of the document tree in a similar manner to the DocumentRoot directive's argument.


1 Answers

Apache typically will pick the first virtual host whose ServerName or ServerAlias matches the host name provided in the Host HTTP header. In your case, since you have no ServerName directives, Apache supposedly uses a reverse DNS lookup on the IP address to fake a server name, and presuming that the reverse DNS leads to domain.com, which doesn't match, Apache then defaults to the first virtual host. Sounds complicated, I know... the bottom line is, you should use ServerName and ServerAlias to make the configuration explicit. Try something more like this:

NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
    ServerName domain.com
    ServerAlias www.domain.com
    VirtualDocumentRoot /var/www/%0
</VirtualHost>
<VirtualHost 127.0.0.1>
    ServerName sub.domain.com
    ServerAlias *.domain.com
    VirtualDocumentRoot /var/www/%2.%3_%1
</VirtualHost>

That should use /var/www/domain.com for http://domain.com and /var/www/www.domain.com for http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.com for http://sub.domain.com, /var/www/blah.domain.com for http://blah.domain.com, and so on.

like image 65
David Z Avatar answered Sep 23 '22 08:09

David Z