Thursday, March 31, 2011

Apache name based virtual hosting

Last time I tried setting up name based virtual hosting on my development machine I just gave up and mapped websites to different ports. This time, I'm trying to do it right :)!

I've properly registered DNS entries and sending two different headers to the server:

Host: dev.site1.com
Host: dev.site2.com

However, Apache is giving me a hard time with mapping dev.site2.com to dev.site1.com

Here's my slimmed down config:

Listen 80
ServerName localhost:80
NameVirtualHost *:80

<VirtualHost *>
    ServerName dev.site1.com
    DocumentRoot /www/site1
</VirtualHost>

<VirtualHost *>
    ServerName dev.site2.com
    DocumentRoot /www/site2
</VirtualHost>

I've tried combos such as (to no effect):

ServerName site1.com
ServerName site2.com

ServerName site1.com
ServerAlias *.site1.com site1
ServerName site2.com site2
ServerAlias *.site2.com site2

I'm happily running Apache 2.2 and hope there's some whose tackled with the same before and can help me out.

From stackoverflow
  • I note the following things:

    1. your ServerName is localhost:81 - shouldn't this be localhost:80?
    2. The DocumentRoot for both VirtualHosts are the same - /www/site1 - so will serve the same pages even if it works

    Try to create a separate DocumentRoot for the second virtual host.

    As a bit of a help, here's my (working, trimmed, URL's renamed) httpd.conf:

    NameVirtualHost *
    
    <VirtualHost *>
            DocumentRoot    /var/www/site1
            ServerName      my.website.co.nz
            ServerAlias     www.my.website.co.nz
    </VirtualHost>
    
    
    <VirtualHost *>
            DocumentRoot    /var/www/joker
            ServerName      another.site.co.nz
            ServerAlias     www.another.site.co.nz
    </VirtualHost>
    

    Happy hunting!

    EDIT: MrTopf has a good point. NameVirtualHost must exactly match the VirtualHost directive. Note how I have an asterisk only for each one of those, whereas you have a NameVirtualHost of *:80 and VirtualHosts with *.

    kRON : Oh sorry, I've misspelled the second document root. Yes, this is exactly what I'm doing, but everything seems to just point to the first VHost entry.
    Fritz H : Can you post your whole config, please?
    MrTopf : yeah, the missing :80 actually got me thinking and digging in the docs. Didn't know before that Apache is that picky ;-)
  • I usually also specify the port in the VirtualHost directives but not sure if this can be the problem:

    <VirtualHost *:80>
        ServerName dev.site2.com
        DocumentRoot /www/site2
    </VirtualHost>
    

    (I even specify the IP address usually in order to make sure it's really listening to the right one in case the server gets an additional one at some point).

    In fact I just noticed in the documentation this line:

    Note that the argument to the directive must exactly match the argument to the NameVirtualHost directive.

    So the problem can be that you specify your NameVirtualHost with a port but the VirtualHost directive without one which eventually does not make them match.

    kRON : Well, great catch! Who would have thought :)? It seemed to be the case. Quite frankly I was sure I got everything right when I read 'ServerName will match the Host parameter from incoming headers', but seems I should have done a better job. Thanks :)!
    MrTopf : Well, it's usually the little things you don't directly spot, isn't it? ;-) Glad it worked :-)

0 comments:

Post a Comment