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.

Running Ubuntu 10.04

My server serves 3 different domains using named virtual hosts in Apache2. I'm current using different Named Virtual Servers to 301 redirect www to the non-www equivalent. It's working, but I don't understand the correct entries for my /etc/hosts file and I think that is causing problems for me trying to setup Varnish.

I understand I need the localhost line

127.0.0.1       localhost localhost.localdomain

Should I also list each domain here? as in

127.0.0.1       localhost localhost.localdomain example1.com example2.com example3.com

What about the entry for the IP of the server? Do I need the following line?

< IP.Of.Server >      example1.com example2.com example3.com

Also, should I be listing www.example.com AND example.com on each line, so they go into Apache and it can deal with the 301 redir?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

I'm assuming this is for testing, otherwise you'd be setting up proper DNS records, not your hosts file.

What you want is for every name you want to call your web server with, to resolve to your server's IP address.

If you are testing from the server itself, then you can make everything point to 127.0.0.1, but of course also making it point to your server's actual IP address would work.

If you are testing from another machine, then of course you want every name to resolve to the server's real IP address.

The syntax is straingthforward:

IP.of.server        www.domain.name domain.name
IP.of.server        www.otherdomain.name otherdomain.name
IP.of.server        www.anotherdomain.name anotherdomain.name
IP.of.server        www.yetanotherdomain.name yetanotherdomain.name

...and so on.


Update:

Of course, what ErikA says is completely right. Modifying the hosts file is not needed for the server to work; it's only useful if/when you need to test it without having proper DNS records in place, or if you want to override them to connect f.e. to a test server instead of a production one.

share|improve this answer
    
I have proper dns records in place, so I'll just remove everything. Thanks –  Dan Grec Aug 11 '11 at 20:25
    
What should I set as "hostname"? –  Dan Grec Aug 11 '11 at 20:25
add comment

The only reason you should ever need to use your hosts file is if you don't have a functioning DNS environment or if you need to temporarily override some DNS name for testing purposes.

Modifying the hosts file is not needed for using Name Virtual Servers.

share|improve this answer
    
I had no idea. I have real DNS mappings set up. So I should just leave the "127.0.0.1 localhost localhost.localdomain" line and not put anyting else in there? –  Dan Grec Aug 11 '11 at 20:23
    
Yep, that should be just fine. No need to add anything else. –  EEAA Aug 11 '11 at 20:26
    
Normally the hosts file on a computer contains only the mappings for localhost and the computer's actual host name. –  Massimo Aug 11 '11 at 20:26
add comment

The IP entry should be whatever Apache is listening on. So if you have <VirtualHost 1.2.3.4:80>, your hosts entry should start with 1.2.3.4.

If you're listening on all your interfaces, then lucky you! You get to pick one! And 127.0.0.1 will work just fine.

As for the part on the right, it must match what you type in the address bar. So if you want to be able to type www.example.com in your address bar and have something happen, then you'll need that name in your hosts file telling your computer which IP that means. Apache can only handle the redirect if the traffic gets there to begin with; hence the hosts file entry.

And I'm sure I needn't say that this is only appropriate for a development setup. Host-IP address mappings should be done by a DNS server, not a static file.

share|improve this answer
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.