简体   繁体   中英

Name based virtual hosts

I'm having trouble figuring out how virtual hosting works. For example lets say in my 'hosts' file I have:

127.0.0.1 localhost
127.0.0.1 mysite1.com mysite1.com mysite3.com

Does this mean that whenever I type in localhost, mysite1.com, mysite2.com, or mysite3.com into my browser URL, the page loaded will be the same for all of them?

The /etc/hosts file only has limited connection to virtual hosting of Apache. The only thing you do with it is give your host (or rather the loop back interace lo ) several names. If you haven't set up anything about virtual hosting yet (which I assume) typing

http://localhost/    
http://mysite1.com/
http://mysite2.com/
http://mysite3.com/

as URL in your browser will all render the same welcome page (provided you have at least set up your Apache) because in all cases the browser will try to access the web server at 127.0.0.1 which your Apache usually listens to.

To create a true virtual hosting you now need to activate this feature in the configuration file of Apache using the tags

<VirtualHost mysite1.com:80>
...
</VirtualHost>
<VirtualHost mysite2.com:80>
...
</VirtualHost>
<VirtualHost mysite3.com:80>
...
</VirtualHost>

The simplest version of a virtual hosting would be that you define a seperate document root for each host and share all other configuration items. The details of this (represented by the ... ), of course, would definitely be out of scope of this answer. A good start for reading would be http://httpd.apache.org/docs/2.2/vhosts/examples.html .

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