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 an Apache web server hosting a few Django sites using the same domain name but different sub-domains:

proj1.example.com
proj2.example.com
proj3.example.com

The apache configuration file of each of the above begins like the following:

<VirtualHost *:80>
    ServerName proj1.example.com

Assuming the external IP of the server is 200.x.x.x and its internal IP is 10.x.x.x Which of those two IP should I use if I want to add a new sub-domain name(proj4.example.com) to the server? I usually do the web dev and basic apache configuration, I'm still fuzzy on what's happening beyond that. Can someone give me a thorough explanation on what need to be setup and what is going on once I have setup the apache configuration file for a new site?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

To quickly answer your question, , you don't need to worry about the IP address for your configuration, the domain name/alias is all you have to worry about, as apache will use the HTTP_HOST header passed by the browser.

<VirtualHost *:80>
    ServerName proj1.example.com
    DocumentRoot /path/to/proj1
</VirtualHost>
<VirtualHost *:80>
    ServerName proj2.example.com
    DocumentRoot /path/to/proj2
</VirtualHost>
<VirtualHost *:80>
    ServerName proj3.example.com
    DocumentRoot /path/to/proj3
</VirtualHost>

Long winded explanation.

In lamen terms, there are 2 seperate things going on here.

  1. resolving a domain name to an IP
  2. sending a web request to a server with virtual hosts set up

Resolving a domain name to an IP

when a browser is told to pull up a page, the first thing it does is do a DNS lookup for proj1.example.com. The DNS requests first it goes to the ROOT servers, and says "who controls example.com", then the root servers point to the DNS server that controls .com domains, and says to that DNS Server and says "who controls example.com", then it goes to your DNS server, and goes "What's the IP address for proj1.example.com". Your DNS Server says, "oh that's IP x.x.x.x", which is the external IP address. The browser will send the request to your IP, at which point, your router forwards it to the your server (with the internal IP address). The IP address of the web server is not part of the HTTP request/response, so it's not needed.

Requesting content from the web server

Your browser now knows that IP address, and sends a request to your iP. The request looks like:

GET http://proj1.example.com/path/being/requested HTTP/1.0
Host: proj1.example.com
User-Agent: [blah blah blah]
Cookie: [cookie info]

The response will look like

HTTP/1.0 200 OK
(new line)
<html>
...
</html>

Apache knows which host the request is for by domain name in the host/url, and pulls the content accordingly.

More documentation is available here: http://httpd.apache.org/docs/2.1/vhosts/name-based.html

Note: The older version of the HTTP protocol didn't support the Host header, so back then, if u wanted to set up virtual hosts on the same machine, it had to use multiple ip address and tie each ip address to a unique site. Some documentation out on the web may still reflect this. In general, tho, you don't have to worry about this as all browsers now support using the Host header

share|improve this answer
    
In "oh that's IP x.x.x.x", is that IP external or internal? –  Thierry Lam Feb 24 '10 at 21:59
    
updated my answer to answer your question –  Roy Rico Feb 24 '10 at 22:05
    
Explanation of the DNS request is wrong: the query to the root name servers (actually, all the queries) includes the full domain name. –  bortzmeyer Mar 1 '10 at 8:15

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.