Forum Moderators: phranque

Message Too Old, No Replies

Any way to use $HOSTNAME or equivalent in VirtualHost?

ServerAlias hostname computername ip address

         

georgekaz

11:56 am on Sep 8, 2010 (gmt 0)

10+ Year Member



Hi,

I have a set-up which involves around 10 servers serving a number of sites behind a load balancer. i.e. All servers serve all sites. The site definitions are distributed from one machine, so every machine has exactly the same contents in it's virtualhosts files.

I'm looking for a solution that allows me to get to a particular site on each machine via it's own IP address or hostname. This allows me to test machines individually. In brief, this is the setup I am after for the default site.

#default site redirects non www, .co.uk and .net domains to www.com domain
<VirtualHost *:80>
ServerName mydomain.com
Redirect 301 / [mydomain.com...]
</VirtualHost>
<VirtualHost *:80>
ServerName www.mydomain.com
[I'm after something like this]
ServerAlias $HOSTNAME
ServerAlias $HOSTNAME.mydomain.com
ServerAlias $IPADDRESS
</VirtualHost>

I just can't work out if there's a way of getting these substitutions for $HOSTNAME and $IPADDRESS in there. Without these aliases in place, the default site redirects back to the main www domain, which sends it back to the load balancer and then on to any of the servers.

Have I made it clear what I'm aiming at?

Thanks in advance

jdMorgan

9:37 pm on Sep 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The mod_alias "Redirect" directive has no way to check the hostname or to use a variable hostname.

So, step one would be to use mod_rewrite instead, with something like:

RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^www\.([^.]+)\.com$
RewriteCond %{HTTP_HOST} ^([^.]+\.)*([^.]+)\.(co\.[a-z]{2}|[a-z]{3,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^/(.*)$ http://www.%2.com/$1 [R=301,L]

This rule "insists" on the hostname starting with only "www" and ending in ".com", but 'keeps' whatever the requested domain name was and "corrects" those other aspects only.

I don't know if sorting this issue will fix your overall problem or not, which you implied had something to do with the redirect itself...

As for variables in the ServerAlias, I know of no way to do that. These are plain-text config files parsed in turn by Apache and by its loaded modules, and not to be confused with scripts. That's why most hosting companies use 'control panel' scripts to auto-generate the config files, so they can add and remove virtualhosts at will, and all of them will be configured identically except for the ServerName and ServerAlias lines.

Jim

georgekaz

7:27 am on Sep 9, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for the reply.

Actually, I had already stopped using Redirect due to the fact that some other local services call the site locally with no domain specified (i.e. using telnet). However, for simplicity in my example here, I still posted with Redirect in order to demonstrate what I was doing. In actuality, my catch all is the following,

<VirtualHost *:80>
ServerName mydomain.com

DocumentRoot /var/www/mysiteroot

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) [mydomain.com$1...] [QSA,R=301,NS,L]
</VirtualHost>

This catches anything that has a domain defined but is not the www.com domain, and forwards it to the www.com domain.

This allows me to forward mydomain.com, mydomain.co.uk, mydomain.net, www.mydomain.co.uk, www.myalternativedomainname.com etc etc, without explicitly mentioning all the possibilities. As you may have guessed from this, I'm handling the default site at that IP address, so if anything comes through to the IP address that's not specifically matched by a VirtualHost, then it get's forwarded to the specified www.com domain. What I'm aiming at is to specifically match the hostname of the machine in a dynamic way and avoid this forwarding and thus the loadbalancer.

So far, I've got around this by specifying a ServerAlias for every machine name in my farm in main domain's VirtualHost definition, and then scp'ing this to all machines. It works, but it's not dynamic if I add a new machine, and it leaves a messy config.

I realise it's not a shell script, but I saw some similar dynamic content on this page

[httpd.apache.org...]

using for example VirtualDocumentRoot /www/commercial/%0/docs

The thing is, I can't find out where %0 is defined, and what other options are available. I was hoping something might be available. Does this sound familiar?

Thanks
George

jdMorgan

10:27 am on Sep 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I'm still a bit fuzzy on this, but perhaps a review of the meanings of the %n tokens defined in mod_vhost_alias [httpd.apache.org] will give you a way out of the trap...

In this case, %0 is the entire ServerName, as defined in the current context.

Jim

georgekaz

10:32 am on Sep 10, 2010 (gmt 0)

10+ Year Member



Ok Jim, thanks for the ideas anyway. Seems I might have to give up on this for now. Maybe someone will find this and post a solution at a later date.

Cheers
George