Monday, June 26, 2017

virtualhost - /etc/hosts entry for single IP server serving multiple domains



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?


Answer



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.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...