httpd.conf - need help with virtual hosts

Discussion in 'NZ Computing' started by a.d., Oct 11, 2005.

  1. a.d.

    a.d. Guest

    Hi

    I've added the <VirtualHost> block like the Apache FAQ says, but it
    doesn't seem to work.

    I am running XAMPP for Windows (Apache, MySQL, PHP, Perl). The Apache
    FAQ says to do 2 things for name-based virtual hosts on a single IP address.

    1) Enable <NameVirtualHost>. Done.
    NameVirtualHost *:80

    2) Add a new <VirtualHost> block for each website I want to host. Done.
    <VirtualHost *:80>
    ServerName sitename1
    DocumentRoot "D:/www/sitename1"
    </VirtualHost>
    <VirtualHost *:80>
    ServerName sitename2
    DocumentRoot "D:/www/sitename2"
    </VirtualHost>

    http://localhost/ works fine, it brings up the default local XAMPP
    website, but if I type http://sitename1/ it runs off and does a Google
    search.

    Can anyone point out what I've done wrong? Am I supposed to disable the
    ServerName and DocumentRoot elements near the top of the httpd.conf file?

    Cheers
     
    a.d., Oct 11, 2005
    #1
    1. Advertisements

  2. a.d.

    a.d. Guest

    I seem to have fixed this but not in an expected way. There is a file
    called "hosts" (no extension) in the windows directory:
    C:\WINXP\system32\drivers\etc\

    It listed the following:
    127.0.0.1 localhost

    I added the following:
    127.0.0.1 sitename1
    127.0.0.1 sitename2

    .... and now it works. Is this the normal way to get VirtualHosts to work
    with Apache in Windows?
     
    a.d., Oct 11, 2005
    #2
    1. Advertisements

  3. a.d.

    Harry Guest

    The DNS lookup of "sitename1" is going to fail unless you
    are running your own local DNS server with a definition in it.
    Perhaps you should try putting a definition in /windows/hosts file.
    Map it to 127.0.0.1 or to the IP of a particular nic.
     
    Harry, Oct 11, 2005
    #3
  4. a.d.

    Shane Guest

    nah
    its not apache having troubles.. its your dns
     
    Shane, Oct 11, 2005
    #4
  5. a.d.

    AD. Guest

    Thats not getting Virtual Hosts to work (they were already working) - the
    hosts thing was so your browser knew where to look for them.

    You would've needed to do the same thing in any other web server.
     
    AD., Oct 12, 2005
    #5
  6. a.d.

    Dave Doe Guest

    Updating the hosts file is um... 'cheating' :) All you're doing is
    creating *local* (per PC) pointers on that windows box to the site. ie
    it'll only work on that PC, and not any other PC on planet earth, sorry.
     
    Dave Doe, Oct 12, 2005
    #6
  7. a.d.

    Shane Guest


    At a wild guess Id say the OP was inside a LAN, and the DNS was pointing
    to his external IP, but needed to be pointed at his internal ip
    And thats why he needed to do his hosts file
     
    Shane, Oct 12, 2005
    #7
  8. a.d.

    Enkidu Guest

    A webserver distinguishes requests for virtual hosts by
    three things, IP address, port, and Host Header Name. A
    client asks for a page by specifying three things (if it is
    HTTP1.1), the IP address, the port and the Host Header Name.

    When you request http://domain1 with your browser, your
    browser first looks up the IP address of 'domain1'. In your
    case the browser is running on the server so your IP address
    is the loopback interface address or 127.0.0.1. Since you
    have put domain name and the IP address in the hosts file,
    the browser can look up 'domain1' and get the correct IP
    addresss, 127.0.0.1.

    Since you use "http" and don't specify a port number the
    default is port 80.

    Since your browser understands HTTP 1.1 it send the Domain
    Name you specified as the "Host Header Name".

    When the server gets the request (which doesn't leave your
    machine!) it has the three bits of information that it needs
    to determine the correct VirtualHost!

    Without the hosts files entries it didn't know that the site
    'domain1' was on the local machine! That's why it went off
    to Google!

    Normally on a Virtual Host machine you would have a number
    of sites each with different names. Each site would have the
    same IP address and would have the same (default) port.

    A remote client would look up the IP address in the usual
    way in DNS and send the request to the default port number
    and the IP address, specifying the Host Header Name in the
    request. The server would then have the three bits of
    information it needs to direct the request to the right
    virtual host.

    Cheers,

    Cliff
     
    Enkidu, Oct 12, 2005
    #8
  9. By the way, Apache itself is quite good at helping you debug these sorts of
    problems. Just Telnet to port 80 on your Web server, and type in an HTTP
    request by hand, e.g.

    GET / HTTP/1.0
    Host: sitename1


    (Press return twice to add a blank line at the end.) It should then spit
    back the home page for the specified virtual site. Similarly try sitename2,
    and see that you get back what you expect.

    It helps to get things working to this level before going to get your DNS
    configured. That way there's only one thing to go wrong at a time. :)
     
    Lawrence D'Oliveiro, Oct 13, 2005
    #9
  10. a.d.

    Enkidu Guest

    HEAD / HTTP/1.1
    Host: sitename

    gets you the header info without the other stuff.

    Cheers,

    Cliff
     
    Enkidu, Oct 14, 2005
    #10
  11. That's not usually enough to tell whether you're getting the right page or
    not.
     
    Lawrence D'Oliveiro, Oct 14, 2005
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.