简体   繁体   中英

Apache2 and Vagrant: localhost mapping

I'm using Vagrant 1.6.3 for running Ubuntu 12.04 virtual machine. On this virtual machine I have Apache 2.4.9. For a long time Apache served only one site- there was 000-default.conf file in the /sites-available directory.

Now I want to add another site. I've created 2 files in /sites-available directory:

  • site1.local.conf
  • site2.local.conf

and I renamed 000-default.conf to default . Also I added site1.local and site2.local hosts to the ./etc/hosts file on the main machine.

Server names in Apache configs are set to site1.local and site2.local correspondently. And when I input http://site1.local:8080/ and http://site2.local:8080/ in browser everything works.

But I don't understand why http://localhost:8080/ still points to the first site? In what config should I define mapping for "localhost"? After setting up server names in apache configs I thought that http://localhost:8080/ should return "Can not connect to server" error.

Assuming you're using a name-based virtual host , site1 and site2 work because the client passes the target host in the header:

If the client provided a Host: header field the list is searched for a matching vhost and the first hit on a ServerName or ServerAlias is taken and the request is served from that vhost.

With localhost , it doesn't know how to match against the defined virtual host entries:

If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected (as already mentioned before).

As far as the last part of your question, you could play around with adding another virtual host entry with localhost as the name definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM