Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple virtual hosts causing problems
#1

hi all i have some virtual webservers in apache and if i leave out the www infront of the address eg: http://windows-noob.com it will open up https://www.linux-noob.com

 

which is quite funny but not the desired result

 

my httpd.conf file looks ok (i think) so where does the problem lie ? with DNS, or with httpd.conf ?

 

NameVirtualHost 94.75.228.161:80

 

<VirtualHost 94.75.228.161:80>

ServerAlias www.linux-noob.com

ServerAdmin anyweb@linux-noob.com

DocumentRoot /websites/linux

ServerName linux-noob.com

 

</VirtualHost>

 

<VirtualHost 94.75.228.161:80>

# ServerAlias www.windows-noob.com

ServerAdmin anyweb@linux-noob.com

DocumentRoot /websites/windows

ServerName www.windows-noob.com

 

 

</VirtualHost>

Reply
#2

I think that because your server name is www.windows-noob.com, it just isn't matching requests for windows-noob.com, simply because the two aren't the same.

 

I actually run my virtual hosts the other way around -- I want them to respond to both the www.peter.upfold.org.uk prefix and no prefix (peter.upfold.org.uk), and then I redirect people silently to peter.upfold.org.uk if they went to www.peter.upfold.org.uk.

 

I do this by setting ServerName to peter.upfold.org.uk and ServerAlias to *.peter.upfold.org.uk. Both prefixed and non-prefixed requests get directed to the correct virtual host, and then I can use a .htaccess rule with mod_rewrite to redirect people to the non-prefixed version.

 

This solution should also be possible the other way round, so that's what I'd recommend here.

 

So, in summary, my recommendation is:

 


  • Set ServerName to windows-noob.com

  • Set ServerAlias to *.windows-noob.com

  • Set a mod_rewrite rule in .htaccess (or in httpd.conf directly) to automatically redirect people to www.windows-noob.com if they come without the prefix.


 

An example of the mod_rewrite rule you might use:

 



Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.windows-noob.com$ [NC]
RewriteRule ^(.*)$ http://www.windows-noob.com/$1 [R=301,L]




Reply
#3

wow excellent answer thanks hybrid !

 

I'll test this out as soon as I can and report back :)

 

thanks again

Reply
#4

A quick footnote here: if the HTTP/1.1 request hits apache and asks for a site that isn't matched in ServerName (or ServerAlias) then the *FIRST* virtual host defined is served up. This often has unexpected effects, such as in your case - DNS is resolving the FQDN to your server, but your server is unable to find any Vhost matching the HTTP request, so defaults to the first one.

 

This also means that the first virtual host often gets plenty of unwanted traffic, some benign (from erroneously-configured DNS records) to malignant (apache port-sniffers).

 

For that reason (and reasons of security), it is a good idea to have the first virtual host dropping requests into a blackhole, eg:



Code:
NameVirtualHost 94.75.228.161:80

## ========== DEFAULT - LEAVE ALONE! ================
<VirtualHost 94.75.228.161:80>

   ## -- DEFAULT: should NEVER get here normally!
   ServerName nothing.here
   ServerAdmin abuse@127.0.0.1
   DocumentRoot /websites/.blackhole

   ErrorLog /var/log/httpd/sniffer_error.log
   CustomLog /var/log/httpd/sniffer_access.log combined
   Loglevel warn

   ScriptAlias /cgi-bin /websites/.blackhole

   ## this redirects any sniffers over to the right page...
   #AliasMatch ^/(.*) /websites/.blackhole/index.php


</VirtualHost>




 



Code:
## =========== LINUX-NOOB.COM =============
<VirtualHost 94.75.228.161:80>

   ServerName linux-noob.com
   ServerAlias www.linux-noob.com
   ServerAdmin anyweb@linux-noob.com

   DocumentRoot /websites/linux

   ErrorLog /var/log/httpd/linux/error.log
   CustomLog /var/log/httpd/linux/access.log combined

</VirtualHost>




 



Code:
## =========== WINDOWS-NOOB.COM =============
<VirtualHost 94.75.228.161:80>

   ServerName windows-noob.com
   ServerAlias www.windows-noob.com
   ServerAdmin anyweb@linux-noob.com

   DocumentRoot /websites/windows

   ErrorLog /var/log/httpd/windows/error.log
   CustomLog /var/log/httpd/windows/access.log combined

</VirtualHost>




 

Also note that many directives at server level (in the httpd.conf file) are usually overridden at vhost level so that vhosts don't pick them up by default - such as aliases for cgi-bin, logging areas, etc.

 

On my servers, I would split mine out into 3 config files (a-default.conf, linux-noob.conf, windows-noob.conf - see above) then use "include" to bring them into my httpd.conf. It means disabling/enabling one site is purely a matter of quickly uncommenting/commenting out one line.

 

Hope that helps (Apache is one of the things I know quite well!)

Reply
#5

excellent info and i'd love to implement this

 

but i'm so lacking with one thing, time,

 

can you please expand on the following as I'm interested in hearing more about it

 

Quote:On my servers, I would split mine out into 3 config files (a-default.conf, linux-noob.conf, windows-noob.conf) then use "include" to bring them into my httpd.conf. It means disabling/enabling one site is purely a matter of quickly uncommenting/commenting out one line.
 

my webserver is serving more than windows-noob and linux-noob but i really need to get them replying properly and i like these ideas, so more info please :)and thanks !

Reply
#6

Yup - it's pretty simple really, and makes administration much easier to manage.

 

I'll take my setup as an example. I have /etc/httpd/ that contains three directories:

conf/

conf.d/

vhosts.d/

 

in conf/ is my httpd.conf file. The last lines in there are:



Code:
Include "/etc/httpd/conf.d/*.conf"
Include "/etc/httpd/vhosts.d/*.conf"




This means that any files ending in ".conf" in those directories are appended to the bottom of apache's config file.

 

In my conf.d/ dir are files specific to certain services - ssl.conf, squirrelmail.conf, mod_security.conf, etc. Many of those get added by installed RPMs; some I rename from .conf to .old or .inc to "disable" them server-wide.

 

In my vhosts.d/ dir are files specific to virtual hosts, such as those mentioned above. I also have some additional files (eg: "shared-icons.inc" and "ie-blocker.inc") that aren't included by default (don't end in .conf) but can still include them INTO an included vhost file - meaning that several vhosts can share common directory locations (such as /images, /icons and the like) - just removed unnecessary duplication.

 

to disable a vhost, just rename it in the vhost.d/ dir from .conf to something else, eg: windows-noob.conf -> window.disabled - then after an apache reload, the config will be overlooked.

 

If you are managing several vhosts, it makes sense to spend some time analysing and planning the config files layout, looking to template your files and normalise duplications (such as shared directory locations).

 

Also: don't forget "/etc/init.d/apache configtest" - use it to test out your config settings before restarting/reloading apache!

 

If it helps, feel free to chuck me a few of your files and I can throw back an example of how I'd structure them, just as an example. They won't fully fit your current setup (can't just drop them in place and hope they work) but it should illustrate what I'm talking about.

Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)