1

I have several IP's which I am trying to configure in Apache2. I have tried searching around but it seems I may be asking for too much.

I want to route every direct request for each IP (e.g. http://155.155.155.100) to a dedicated DocumentRoot (e.g. /www/155.155.155.00/).

While domains which resolve to the same IP (155.155.155.100) will have a separate DocumentRoot. So far, Apache has no problems routing IP's, However it doesnt like sharing them between domains.

My setup so far (Does not work, Both IP and domain route to /www/155.155.155.101 - they are not separate):

NameVirtualHost 155.155.155.100
NameVirtualHost 155.155.155.101
NameVirtualHost 155.155.155.102
NameVirtualHost 155.155.155.103

<VirtualHost 155.155.155.101>
DocumentRoot /www/155.155.155.101
</VirtualHost>

<VirtualHost 155.155.155.101>
ServerName www.domain1.com
ServerAlias www.domain1.com
DocumentRoot /www/domain1.com
</VirtualHost>

I'd appreciate help!

2 Answers2

1

Untested. I assumed your hostnames were each on a single IP; if not, you can use a single NameVirtualHost * instead, and match on <VirtualHost *> everywhere.

NameVirtualHost 155.155.155.100:80
NameVirtualHost 155.155.155.101:80
NameVirtualHost 155.155.155.102:80
NameVirtualHost 155.155.155.103:80

<VirtualHost 155.155.155.100:80>
ServerName 155.155.155.100
DocumentRoot /www/155.155.155.100
</VirtualHost>

<VirtualHost 155.155.155.100:80>
ServerName www.domain0.com
DocumentRoot /www/domain0.com
</VirtualHost>

<VirtualHost 155.155.155.101:80>
ServerName 155.155.155.101
DocumentRoot /www/155.155.155.101
</VirtualHost>

<VirtualHost 155.155.155.101:80>
ServerName www.domain1.com
DocumentRoot /www/domain1.com
</VirtualHost>

As far as I can tell your mistake was in not giving a ServerName to the first vhost.

Apache used the reverse of the IP that received the request, which I expect was www.domain1.com; your second virtualhost was therefore hidden.

Tobu
  • 4,437
  • 1
  • 24
  • 31
  • 1
    In addition: remember that the first specified vhost is used if nothing matches. So be sure you define a sensible fallback vhost _first_. – Niels Basjes Jul 02 '10 at 21:11
0

The problem is that your first IP only stanza,

<VirtualHost 155.155.155.101>
    DocumentRoot /www/155.155.155.101
</VirtualHost>

does not include a ServerName or ServerAlias while the one following it with the domain does. Apache will attempt to match an incoming connection to the first VirtualHost configuration stanza that matches. As a connection using a domain that resolves to the IP will match the IP without any further refinement (ServerName or ServerAlias) it gets used to handle the connection.

You should therefore either ensure that each VirtualHost config stanza has a ServerName or ServerAlias option along with the DocumentRoot or you should make sure your more specific VirtualHost config stanzas appear first in the config file before the less specific ones (ie- the IP addresses themselves with or without the ServerName option.

Thus the following should function as you expected:

NameVirtualHost 155.155.155.100
NameVirtualHost 155.155.155.101
NameVirtualHost 155.155.155.102
NameVirtualHost 155.155.155.103

<VirtualHost 155.155.155.101>
    ServerName www.domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /www/domain1.com
</VirtualHost>

<VirtualHost 155.155.155.101>
    DocumentRoot /www/155.155.155.101
</VirtualHost>
Jeremy Bouse
  • 11,341
  • 2
  • 28
  • 40