1

I just dont get how my browser is able to route to correct path....when I give ServerName of a virtual host....even though it is not configured in "/etc/hosts"

Please explain me.....

This is the Virtualhost Iam talking abt

<VirtualHost *:80>
  ServerName example.com
  ServerAlias example
  DocumentRoot "<*path*>"
  RailsEnv development
  <Directory "<*path*>"
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
5
  • what is the IP of your server
    – LukeR
    Dec 11, 2009 at 9:44
  • its on localhost
    – user28819
    Dec 11, 2009 at 10:31
  • That's not an IP address...
    – LukeR
    Dec 11, 2009 at 10:45
  • I mean....my server is on local machine....i.e., 127.0.0.1
    – user28819
    Dec 11, 2009 at 11:27
  • I'm beginning to doubt the validity of this question being asked here. I would go out on a limb and suggest you aren't a sysadmin. serverfault.com/faq
    – LukeR
    Dec 12, 2009 at 2:23

2 Answers 2

5

The DNS server doesn't take ServerName from httpd.conf

Your browser doesn't route the request, the web server returns the correct page based in part on the Host HTTP Header sent by the browser.

When you type a URL in your browser and hit enter, the name is resolved to IP using DNS, and the browser sends an HTTP request to the IP returned from the DNS query.

HTTP headers form the core of the HTTP request. One of these is the Host, or domain name you typed in the browser.

Using an example Virtual Host definition.

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/example.com
</VirtualHost>

The ServerName directive within your Virtual Host configuration is matched against the Host HTTP Header. So if you type

http://example.com

Apache will receive the request and try to match the Host header

Host: example.com

with it's Virtual Host definitions. If it finds a a match, it will the use whatever is listed in the

DocumentRoot 

In the example above

/var/www/example.com

This could be either an index file as defined within the configuration, or a directory index if they are permitted.

http://en.wikipedia.org/wiki/Virtual_hosting#Name-based

7
  • Thanks for your response....but my question is before apache to get the request....IP address should get resolved right....which is done by contacting DNS server or using "/etc/hosts"....but in my case...I have apache server on my local machine and a named virtual host....when I type ServerName in my browser...how is it able to resolve....I did not add any mapping to "/etc/hosts" file....
    – user28819
    Dec 11, 2009 at 6:56
  • How does it know that the ServerName entered is my local machine??
    – user28819
    Dec 11, 2009 at 6:57
  • What are you typing into your browser? What is the ServerName directive?
    – LukeR
    Dec 11, 2009 at 7:13
  • I mean to say....I submit "example.com" in the web browser.... My question is just...."How does IP - Domainname(example.com) mapping is done" here....though I did not change /etc/hosts
    – user28819
    Dec 11, 2009 at 8:28
  • I understand your question, but without specifics as to what you're typing and what your VirtualHosts config is, I can't answer it. I can guarantee if you type example.com into a browser with an empty hosts file, it's not showing your local apache server.
    – LukeR
    Dec 11, 2009 at 8:40
0

I believe it is actually taking it from wherever your OS is storing the hostname, /etc/hostname on Ubuntu or /etc/sysconfig/network under RH-type distros

During install, I think it fills ServerName in by running the hostname command

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .