Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have several web sites, all set up in http-vhosts.conf with their respective ServerName and DocumentRoot, they are working fine and dandy.

But when I visit the server's IP address URL, I get back the files in the first virtual host, rather than Apache serving from where I would expect, the http.conf file's DocumentRoot.

How can I configure Apache so visiting the server by IP address, whether external or loopback, does not return the first VirtualHost?

Here's the first VirtualHost, which I'm currently getting back as the response if I visit the web server by IP:

<VirtualHost *:80>
    ServerName "delaneatallent.com"
    ServerAlias "www.delaneatallent.com"
    DocumentRoot "/Library/WebServer/Documents/delanea"
</VirtualHost>

Using Apache 2.2.14 on OS X, if that matters.

:wq

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

In short, you can't - that's how Apache works. If you are listening on that IP (as per Prix's comment) then Apache will use the first virtual host as defined when serving up an IP request.

If this is undesirable, then consider changing the first virtual host to be what you actually want served if it's by IP. If that is not what you want, then consider putting in redirect as the first virtual host to make it go somewhere you do want.

# default
<VirtualHost *:80>
  RewriteEngine on
  RewriteRule ^/(.*) http://othersite.com/$1 [L,R]
</VirtualHost>

You can't stop Apache from listening on that IP, but you can make it do something else that does do what you want with a little creativity.

share|improve this answer
    
Thanks Troy! I didn't want to create a fake VirtualHost as a patch if the cause was something else in my config. Works fine now. –  richardtallent Aug 29 '10 at 4:56
add comment

What I did to remedy this was to do a default page for /var/www and that serves the default site you see when you navigate to my server by IP or unknown url related to a site on my server.

The actual sites that I host are in the sites-enabled folder with the default site configuration, all name based and pointing to a different directory.

Long story short, navigate to my server by its IP, you get the default error page.

share|improve this answer
add comment

To make your apache listen to specific ip and port you must define it:

Listen 10.0.0.1:80
Listen 20.0.0.1:80

Where 20.0.0.1 and 10.0.0.1 are 2 example ips i have assigned to it.

If you already have another Listen configured you may need to change it, for example:

Listen 0.0.0.0:80 

would make your apache listen on all interfaces available.

share|improve this answer
    
Listen is set to "80", server is serving files fine. Problem is that when visiting the site by IP address, the first VirtualHost is returned rather than the expected DocumentRoot. –  richardtallent Aug 28 '10 at 18:02
    
that is how apache works it will always show the documentroot for the 1st virtualhost. if u dont want that you can create a new 1st virtualhost there is nothing you can really do about it. For example if u listen to 2 ips when those 2 ips are typed they will take you to the document root. –  Prix Aug 28 '10 at 20:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.