5

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?

Michael Lowman
  • 3,604
  • 20
  • 36
Dan Grec
  • 65
  • 2
  • 4

4 Answers4

6

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.

Massimo
  • 70,200
  • 57
  • 200
  • 323
2

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.

EEAA
  • 109,363
  • 18
  • 175
  • 245
  • 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
  • My vps has a automatically generated Hostname in there. I'm going to change it out for my .com because some of the bash command line functions in centos appear to read from there and return a value to other scripts. It was a weird thing to figure out. See hostname (7). – Sinthia V Jul 19 '16 at 20:28
1

Apparently you do always need to fix the hosts file, regardless of DNS - otherwise when you re-start apache under heavy load, it takes up to 90 seconds to actually stop and re-start ( see Why httpd graceful restart takes such a long time? )

cnd
  • 169
  • 4
1

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.

Michael Lowman
  • 3,604
  • 20
  • 36