Handling site not found and page not found with dynamic mass virtual hosting
Posted
by
Rick Moynihan
on Server Fault
See other posts from Server Fault
or by Rick Moynihan
Published on 2012-08-31T15:28:46Z
Indexed on
2012/08/31
15:41 UTC
Read the original article
Hit count: 353
I have recently setup mass virtual hosting in Apache so that all we need to do is create a directory to create a new vhost. We're then also using wildcard DNS to map all subdomains to the server running our Apache instance.
This works excellently, however I'm now having trouble configuring it to fail-over to an appropriate default/error-page when the vhost directory does not exist.
The problem appears to be conflated between by my desire to handle the two error conditions:
- vhost not found i.e. there was no directory found matching the host supplied in the HTTP host header. I'd like this to display an appropriate site not found error page.
- The 404 page not found condition of the vhost.
Additionally I have a specialised "api" vhost in its own vhost block.
I've tried a number of variations and none seem to exhibit the behaviour I want. Here's what I'm working with right now:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/site-not-found
ServerName sitenotfound.mydomain.org
ErrorDocument 500 /500.html
ErrorDocument 404 /500.html
</VirtualHost>
<VirtualHost *:80>
ServerName api.mydomain.org
DocumentRoot /var/www/vhosts/api.mydomain.org/current
# other directives, e.g. setting up passenger/rails etc...
</VirtualHost>
<VirtualHost *:80>
# get the server name from the Host: header
UseCanonicalName Off
VirtualDocumentRoot /var/www/vhosts/%0/current
# other directives ... e.g proxy passing to api etc...
ErrorDocument 404 /404.html
</VirtualHost>
My understanding is that the first vhost block is used as the default, so I have this here as my catch all site. Next I have my API vhost, and then finally my mass vhost block.
So for a domain that doesn't match the first two ServerName's and has no corresponding directory in /var/www/vhosts/
I'd expect it to fall-over to the first vhost, however with this setup, all domains resolve to my default site-not-found. Why is this?
By putting the mass-vhost block first, I can get the mass-vhosts to resolve properly, but not my site-not-found vhost... and in this case I can't seem to find a way to distinguish between a page-level 404 in the vhost, and the case where the VirtualDocumentRoot fails to find a vhost directory (this appears to use the 404 also).
Any help out of this bind is much appreciated!
© Server Fault or respective owner