Filip Dupanović
Filip Dupanović

Reputation: 33640

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.

Upvotes: 1

Views: 2014

Answers (2)

Fritz H
Fritz H

Reputation: 3569

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 *.

Upvotes: 4

MrTopf
MrTopf

Reputation: 4853

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.

Upvotes: 4

Related Questions