You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by wayne <wa...@jawnee.org> on 2009/05/06 04:04:39 UTC

[users@httpd] dynamic/mass virtual hosts with www.* alias and localhost...

ahoy all,

    	i have been trying/searching/etc unsuccessfully to do the following
(what i believe to be) simple task:

enable mass/dynamic virtual hosts, allowing each virtual host to be
accessed via example.com AND www.example.com, without creating a
VirtualHost directive for each host.

    	my attempts have centered on first enabling dynamic virtual hosting
with:

NameVirtualHost *
UseCanonicalName Off
VirtualDocumentRoot /var/www/sites/%0/www
VirtualScriptAlias /var/www/sites/%0/cgi-bin

which works fine.  i can access all the sites listed in /var/www/sites/
(once they are added to /etc/hosts of course, in both their
www.example.com and example.com forms).  then, i attempt to make a
VirtualHost directive for the default host that lists the alias:

<VirtualHost *>
	ServerName default
	ServerAlias www.*
	DocumentRoot /var/www/
</VirtualHost>

unfortunately this does nothing.  the virtual hosts are only accessible
via their domain names WITHOUT the www. prefix.

	in addition, i can no longer access the default DocumentRoot via
localhost or 127.0.0.1.  i believe this is a hint to my problem, as it
seems the VirtualHost directive is not overriding the dynamic/mass
virtual hosting.

	here are my details: running Apache/2.2.8 (Ubuntu) Mar 10 2009 18:09:05
on Ubuntu Hardy 8.04 with kernel 2.6.24-23 and all latest updates
applied.  i have tried it with/without the default "sites-enabled"
Debian config, to no luck.

	i would appreciate any help with this, or if it is even possible.  it
has been a few days of trying with no luck.

thanks, wayne.



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] dynamic/mass virtual hosts with www.* alias and localhost...

Posted by Rainer Frey <ra...@inxmail.de>.
On Thursday 07 May 2009 16:08:01 wayne wrote:
> ahoy,

>     also, once dynamic/mass virtual hosting is enabled, i can no longer
> access the "default" site of localhost or 127.0.0.1.  i have tried to
> use a VirtualHost directive to setup a default host (i.e. localhost),
> but with no luck.
>
>     i mainly will be using dynamic hosts, though i would like to have
> the default localhost site work (if possible) as well.  also, i was
> hoping to be able to customize certain dynamic virtual hosts with
> specific VirtualHost directives.

You need to understand that there is no localhost/127.0.0.1 specific site in 
Apache. If the Server has different IP addresses, or Apache listens to 
different ports, the IP address and port to which the 
client connected are matched against the <VirtualHost> directives. 

If there is a matching NameVirtualHost directive for this address and port, 
the actual virtual Host is selected by the Name specified in the Host header, 
or if none matches, the first VHost with this address is used. As I mentioned 
before, NameVirtualHost does not work in the same context as mod_vhost_alias.

Otherwise, the VirtualHost is used directly. If no VHost matches address and 
port, but a VHost with the magic name _default_ exists for the port, that is 
used. If this also does not exist, the main server config is used.

At that point, you are now inside a VHost or the main server context. If 
within this context, a VirtualDocumentRoot is found, it is matched against 
the Host header to determine the document root and the server name. Otherwise 
theDocumentRoot and ServerName directives are used. Note that there is no 
fallback if VirtualDocumentRoot does not match, an error is returned instead. 
So you need to use seperate VHosts for the default site and the dynamic 
VHosts.

See http://httpd.apache.org/docs/2.2/vhosts/ and esp. 
http://httpd.apache.org/docs/2.2/vhosts/details.html for in-depth 
explanation.

So you best remove all your configuration attempts, create a 
file /etc/apache2/sites-available/dynamic-vhosts with following content, and 
enable it with a2ensite dynamic-vhosts (assuming you do want to use the 
debian config):

-------
UseCanonicalName Off
<VirtualHost ip.address.where.yourvhost.domains.point.to>
	VirtualDocumentRoot /var/web/sites/%-2.0.%-1/www
	VirtualScriptAlias /var/web/sites/%-2.0.%-1/cgi-bin

        <Directory /var/web>
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>
--------

Note that I used /var/web instead of /var/www to ensure that the dynamic sites 
do not overlap with the default document root. Otherwise all the sites would 
be available as subdirectories of the default site.

Now, on the IP address used for the dynamic sites, all those sites are 
available, and on any other IP (including localhost), the default site is 
served.

Remember that you need to add the domain names for the site to DNS, not 
the /etc/hosts of the Apache server. Otherwise the clients can't resolve 
them :-)

HTH
Rainer



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] dynamic/mass virtual hosts with www.* alias and localhost...

Posted by wayne <wa...@jawnee.org>.
On Thu, 2009-05-07 at 10:19 -0400, Eric Covener wrote:
> On Thu, May 7, 2009 at 10:08 AM, wayne <wa...@jawnee.org> wrote:
> > ahoy,
> >
> >     this is the first problem: unless i create a link with
> 
> > "www.example1.org" to the "example1.org" directory of sites, apache will
> > only server requests to example1.org, not www.example1.org .  thus, i am
> > trying to setup some directive to avoid having to make 2 entries (both the
> > domain.org directory AND a www.domain.org link to the directory) to have
> > apache serve both domains.
> 
> Can you use mod_rewrite to force a canonocal flavor of each domain
> with always www or no-www?
> 
    i assume i could do that, though mod_vhost_alias seems to be the
favored method over mod_rewrite for doing this.  also, using a directory
interpolation of /var/www/sites/%-2.0.%-1 works perfectly, as suggested
by Rainer.

thanks nonetheless.



---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] dynamic/mass virtual hosts with www.* alias and localhost...

Posted by Eric Covener <co...@gmail.com>.
On Thu, May 7, 2009 at 10:08 AM, wayne <wa...@jawnee.org> wrote:
> ahoy,
>
>     this is the first problem: unless i create a link with

> "www.example1.org" to the "example1.org" directory of sites, apache will
> only server requests to example1.org, not www.example1.org .  thus, i am
> trying to setup some directive to avoid having to make 2 entries (both the
> domain.org directory AND a www.domain.org link to the directory) to have
> apache serve both domains.

Can you use mod_rewrite to force a canonocal flavor of each domain
with always www or no-www?

-- 
Eric Covener
covener@gmail.com

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] dynamic/mass virtual hosts with www.* alias and localhost...

Posted by wayne <wa...@jawnee.org>.
ahoy,

    first, thanks for the reply.  i apologize for the confusion of my
question.  i will try to clarify and address the issues in-line and
below.

    what i am trying to do is use apache's dynamic/mass virtual host in
the following manner:  to add a host, i would like to simply:

1. create a directory or link in my /var/www/sites/ directory (e.g.
example1.org) with the site content
2. add the domain name to /etc/hosts (e.g. 127.0.0.0.1    example1.org)

and have apache start serving up the correct pages for both
http://example1.org AND http://www.example1.org .

    this is the first problem: unless i create a link with
"www.example1.org" to the "example1.org" directory of sites, apache will
only server requests to example1.org, not www.example1.org .  thus, i am
trying to setup some directive to avoid having to make 2 entries (both
the domain.org directory AND a www.domain.org link to the directory) to
have apache serve both domains.

    also, once dynamic/mass virtual hosting is enabled, i can no longer
access the "default" site of localhost or 127.0.0.1.  i have tried to
use a VirtualHost directive to setup a default host (i.e. localhost),
but with no luck.

    ok, let me try your suggestions....  ok, my results are below
in-line:

On Wed, 2009-05-06 at 09:01 +0200, Rainer Frey wrote:

> Hi,
> 
> actually your description of you want to achieve, and what already works is a 
> bit confusing.  Do you want to serve the dynamic hosts only, or do you have 
> an additional default site?

    i am trying to do both: server dynamic hosts, and have a default
site at localhost.

> 
> More comments inline.  
> 
> On Wednesday 06 May 2009 04:04:39 wayne wrote:
> > enable mass/dynamic virtual hosts, allowing each virtual host to be
> > accessed via example.com AND www.example.com, without creating a
> > VirtualHost directive for each host.
> >
> 
> > NameVirtualHost *
> 
> This is not needed for dynamic vhosts, and has no influence on the 
> configuration that is outside any vhost directive.

    ok, makes sense after reading your comments.

> 
> > UseCanonicalName Off
> > VirtualDocumentRoot /var/www/sites/%0/www
> > VirtualScriptAlias /var/www/sites/%0/cgi-bin
> >
> > which works fine.  i can access all the sites listed in /var/www/sites/
> > (once they are added to /etc/hosts of course, in both their
> > www.example.com and example.com forms).  
> 
> So at that point, what does work? Only the domain, or also the www.domain?

    only the domain works, not www.domain.

> I assume that the latter does not work. You have two ways:
> 1. change the directory  interpolation of the VirtualDocumentRoot and  
> VirtualScriptAlias e.g. to   /var/www/sites/%-2.0.%-1
> that means the directory name is the second last domain part, a dot, and then 
> the last domain part. This enables www.domain as well as any subdomain that 
> resolves to your apache.
> 2. simply symlink the domain directory, e.g. www.domain.tld -> domain.tld and 
> leave the directory interpolation as is.

    Eureka!  ok, yes, the solution for the www. problem was to use the
directory interpolation you suggested.  so now, apache serves for both
example1.org AND www.example1.org using a single directory in
the /var/www/sites directory.  thanks!  so simple, sorry i missed that.
i though the answer lay in some directive.  now on to problem 2...

> 
> > then, i attempt to make a 
> > VirtualHost directive for the default host that lists the alias:
> >
> > <VirtualHost *>
> > 	ServerName default
> > 	ServerAlias www.*
> wildcards are only supported for the first domain part of the alias AFAIK.
> > 	DocumentRoot /var/www/
> I think it is a bad idea to have (Virtual)DocumentRoots of hostzs overlap each 
> other.

    i agree, but i was desperate!  your interpolation solution above
made this attempt to setup a default configuration unnecessary.
danke/thanks again!

> > </VirtualHost>
> > unfortunately this does nothing.  the virtual hosts are only accessible
> > via their domain names WITHOUT the www. prefix.
> 
> What is this this VHost supposed to do? Define the default site, or provide 
> common configuration for the dynamic vhosts? 

    i was hoping both?

> The latter thas not work at all, 
> dynamic  vhosts have nothing to do with a <VirtualHost> container. 

    ok, again makes sense after reading your comments.

> You also 
> cannot define ServerAliases for the dynamic vhosts at all, use directory 
> interpolation or symlinks as described above.
> 
> > 	in addition, i can no longer access the default DocumentRoot via
> > localhost or 127.0.0.1. 
> 
> Sure, you defined NameVirtualHost * and <VirtualHost *>. All requests are 
> directed to VHosts, none reaches the main server configuration

    ok, that also makes sense.  however, i have removed all
NameVirtualHost and VirtualHost directives.  now, the dynamic virtual
hosting still works, but the default sites of localhost / 127.0.0.1
still do not work (i.e. http://localhost and http://127.0.0.1 both
return 404).  both of these domains work if dynamic virtual hosting is
disabled.  i also tried setting a "default" DocumentRoot in http.conf,
with the same results.  any other ideas?  i even tried making a
"localhost" directory in the /var/www/sites/ directory, still with no
luck.

> 
> > 	here are my details: running Apache/2.2.8 (Ubuntu) Mar 10 2009 18:09:05
> > on Ubuntu Hardy 8.04 with kernel 2.6.24-23 and all latest updates
> > applied.  i have tried it with/without the default "sites-enabled"
> > Debian config, to no luck.
> 
> If you use the debian package, I'd recommend using its site configuration 
> mechanism, it is quite convenient. If you only need the dynamic vhosts, put 
> the config in the default site. If you have one or more other sites, put the 
> dynamic vhosts inside a <VirtualHost> with a specific IP address.

    i mainly will be using dynamic hosts, though i would like to have
the default localhost site work (if possible) as well.  also, i was
hoping to be able to customize certain dynamic virtual hosts with
specific VirtualHost directives.

thanks again, the first problem is solved thanks to you...  and sorry
for the confusing questions.

peace, wayne


> 
> Rainer
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
> 

Re: [users@httpd] dynamic/mass virtual hosts with www.* alias and localhost...

Posted by Rainer Frey <ra...@inxmail.de>.
Hi,

actually your description of you want to achieve, and what already works is a 
bit confusing.  Do you want to serve the dynamic hosts only, or do you have 
an additional default site?

More comments inline.  

On Wednesday 06 May 2009 04:04:39 wayne wrote:
> enable mass/dynamic virtual hosts, allowing each virtual host to be
> accessed via example.com AND www.example.com, without creating a
> VirtualHost directive for each host.
>

> NameVirtualHost *

This is not needed for dynamic vhosts, and has no influence on the 
configuration that is outside any vhost directive.

> UseCanonicalName Off
> VirtualDocumentRoot /var/www/sites/%0/www
> VirtualScriptAlias /var/www/sites/%0/cgi-bin
>
> which works fine.  i can access all the sites listed in /var/www/sites/
> (once they are added to /etc/hosts of course, in both their
> www.example.com and example.com forms).  

So at that point, what does work? Only the domain, or also the www.domain?
I assume that the latter does not work. You have two ways:
1. change the directory  interpolation of the VirtualDocumentRoot and  
VirtualScriptAlias e.g. to   /var/www/sites/%-2.0.%-1
that means the directory name is the second last domain part, a dot, and then 
the last domain part. This enables www.domain as well as any subdomain that 
resolves to your apache.
2. simply symlink the domain directory, e.g. www.domain.tld -> domain.tld and 
leave the directory interpolation as is.

> then, i attempt to make a 
> VirtualHost directive for the default host that lists the alias:
>
> <VirtualHost *>
> 	ServerName default
> 	ServerAlias www.*
wildcards are only supported for the first domain part of the alias AFAIK.
> 	DocumentRoot /var/www/
I think it is a bad idea to have (Virtual)DocumentRoots of hostzs overlap each 
other.
> </VirtualHost>
> unfortunately this does nothing.  the virtual hosts are only accessible
> via their domain names WITHOUT the www. prefix.

What is this this VHost supposed to do? Define the default site, or provide 
common configuration for the dynamic vhosts? The latter thas not work at all, 
dynamic  vhosts have nothing to do with a <VirtualHost> container. You also 
cannot define ServerAliases for the dynamic vhosts at all, use directory 
interpolation or symlinks as described above.

> 	in addition, i can no longer access the default DocumentRoot via
> localhost or 127.0.0.1. 

Sure, you defined NameVirtualHost * and <VirtualHost *>. All requests are 
directed to VHosts, none reaches the main server configuration

> 	here are my details: running Apache/2.2.8 (Ubuntu) Mar 10 2009 18:09:05
> on Ubuntu Hardy 8.04 with kernel 2.6.24-23 and all latest updates
> applied.  i have tried it with/without the default "sites-enabled"
> Debian config, to no luck.

If you use the debian package, I'd recommend using its site configuration 
mechanism, it is quite convenient. If you only need the dynamic vhosts, put 
the config in the default site. If you have one or more other sites, put the 
dynamic vhosts inside a <VirtualHost> with a specific IP address.

Rainer

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org