# Slightly more specific Apache VHOST issue [SOLVED]

## Remillard

Well, I've gone and done some more tinkering and I think I've got it down to a basic issue.

To recap, I am trying to have several different hosts setup locally for testing (well at the moment 2, but more later).  Currently I have host 'localhost' and host 'gyldenholt'.  I would like to be able to aim a browser at http://localhost and get that page, and http://gyldenholt and get that page (natch).

Currently, they both resolve to the localhost index.html page.

So, in vhosts.conf I have:

```
NameVirtualHost *:80

<VirtualHost *>

DocumentRoot /var/www/localhost/htdocs

ServerName localhost

#ServerPath /localhost

</VirtualHost>

<VirtualHost *>

DocumentRoot /var/www/gyldenholt/htdocs

ServerName gyldenholt

#ServerPath /gyldenholt

</VirtualHost>
```

I had to make NameVirtualHost *:80 because otherwise Apache complains about _default_:443 and NameVirtualHost * together in the same configuration.  

Now then, I try both of these, and as I said, it serves up the localhost index.html file.  I check the access log and it reads:

```
127.0.0.1 - - [29/Jul/2004:12:30:40 -0700] "GET / HTTP/1.1" 200 1473 "http://localhost/" "ELinks (0.9.1; Linux 2.6.3-rc2-mm1 i686; 144x54)"

127.0.0.1 - - [29/Jul/2004:12:30:48 -0700] "GET / HTTP/1.1" 200 1473 "http://gyldenholt/" "ELinks (0.9.1; Linux 2.6.3-rc2-mm1 i686; 144x54)"
```

So, as far as I can tell, Apache ought to know what the hostname requested is.

Is there something else I can do to try to track this down?  I got a lot of good information about VHOST from the PHP Editor Review and of course the prior help in the forum.

The only thing I can think that might be confusing apache is that in hosts I have both localhost and gyldenholt mapped to 127.0.0.1.  However as far as I can tell, the whole point of named virtual hosts is to be able to have multiple hostnames assigned to one webserver, and the server being able to distinguish between the two.

Any ideas out there on this one?

Regards,

Remillard

----------

## intgr

Try this:

```

NameVirtualHost *:80

<VirtualHost localhost:80 127.0.0.1:80>

DocumentRoot /var/www/localhost/htdocs

ServerName localhost

#ServerPath /localhost

</VirtualHost>

<VirtualHost gyldenholt:80>

DocumentRoot /var/www/gyldenholt/htdocs

ServerName gyldenholt

#ServerPath /gyldenholt

</VirtualHost>

```

This ought to work. Be careful with 'NameVirtualHost *:80', because if a machine, that is not really 127.0.0.1, provides 'localhost' for the 'Host:' header, Apache will choose that VirtualHost instead.

The obvious answer to this would be to use IP-based virtualhosts (you can still have multiple named vhosts for the external IP). I'll use 192.168.0.1 as an example for the remote IP of your machine:

```

NameVirtualHost 192.168.0.1:80

<VirtualHost 127.0.0.1:80>

DocumentRoot /var/www/localhost/htdocs

ServerName localhost

</VirtualHost>

<VirtualHost gyldenholt:80 192.168.0.1:80 _default_:80>

DocumentRoot /var/www/gyldenholt/htdocs

ServerName gyldenholt

</VirtualHost>

```

----------

## Remillard

Wow, great.  This is the first serious advance I've gotten on this problem.

Okay to the first, it almost works.  I made a typo such that it couldn't resolve localhost, and as such then localhost and gyldenholt both mapped to the gyldenholt directory.  Then I fixed it and I get the following.  I think it makes sense, but I'm not sure how to correct it.

```
[Thu Jul 29 14:06:43 2004] [warn] VirtualHost localhost:80 overlaps with VirtualHost gyldenholt:80, the first has precedence, perhaps you need a NameVirtualHost directive

[Thu Jul 29 14:06:43 2004] [warn] VirtualHost 127.0.0.1:80 overlaps with VirtualHost localhost:80, the first has precedence, perhaps you need a NameVirtualHost directive
```

So as far as I can tell this is exactly why I was having the trouble in the first place, they were both mapping the same.

However, this is how it's supposed to work I think.  It ought to be reading the hostname.  In this instance, once again, both gyldenholt and localhost map to the localhost files (as the warning suggested would happen).  

As for the IP based mapping, this might work out okay.  While I'm behind a router and really have no desire to run as a public server, I do have an internal IP address.  However, a question... do I then go into /etc/hosts and map gyldenholt to 192.168.0.1 so that the name will resolved to this machine?  And secondly, if I add something like 'testsite' to the machine, will the gyldenholt VirtualHost entry override THAT hostname?

----------

## intgr

 *Remillard wrote:*   

> 
> 
> However, a question... do I then go into /etc/hosts and map gyldenholt to 192.168.0.1 so that the name will resolved to this machine?
> 
> 

 

Yes, you should make gyldenholt resolve to the IP of your LAN network interface

 *Remillard wrote:*   

> And secondly, if I add something like 'testsite' to the machine, will the gyldenholt VirtualHost entry override THAT hostname?

 

No, it works for me. Apache might override 'NameVirtualHost' for localhost, but Apache's site is really slow for me today, so I couldn't look it up from the docs.

----------

## Remillard

Well should be easy enough to try, I'll see what happens.

--------------------

Post-try.

This actually works like a charm.  I'm not sure why the named virtual hosting didn't work but this does just fine.  For posterity and those who might come after, here's what I did.

In hosts, I have:

```
# Local webs

192.168.0.32     gyldenholt

192.168.0.32     newsite

```

In /etc/apache2/conf/vhosts/vhosts.conf, I have:

```
NameVirtualHost 192.168.0.32:80

<VirtualHost 127.0.0.1:80>

DocumentRoot /var/www/localhost/htdocs

ServerName localhost

#ServerPath /localhost

</VirtualHost>

<VirtualHost gyldenholt:80 192.168.0.32:80 _default_:80>

DocumentRoot /var/www/gyldenholt/htdocs

ServerName gyldenholt

#ServerPath /gyldenholt

</VirtualHost>

<VirtualHost newsite:80>

DocumentRoot /var/www/newsite/htdocs

ServerName newsite

</VirtualHost>

```

And then I have all the usual stuff you have to do for virtual hosting like UseCanonicalNames Off, and making sure the directory accesses in commonapache2.conf are set up correctly.

For Named Virtual Hosting, perhaps localhost is a special 'feature'.

Anyway, thank you VERY much.

Regards,

Remillard

----------

## intgr

 *Remillard wrote:*   

> Anyway, thank you VERY much.

 

You're welcome.  :Smile: 

Don't forget to append '[SOLVED]' to the thread subject.

----------

## CoolAJ86

Too many people end there last post with "Nevermind, I figured it out." Thank you SO much for coming back and wrapping it up. I've been having a headache trying to do the exact same thing (interestingly enough I was searching for an ENTIRELY different issue when I landed on this). It looks like I might get it all set up now... although I have one minor additional complication. I want the addresses to be available on port 80 and 4887 (my ISP blocks port 80 goind out).

----------

## d0wn_under

This just fixed the same problem I was having on apache on a FreeBSD box.

Gentoo offers the best support for everything!

----------

