HTTPD supported virtual host types include the following three kinds of
- Domain-based: uses a different domain name for each virtual host, but its corresponding IP makes the same.
- Based on IP address: For each virtual host to use a different domain name, the corresponding IP address is not the same.
- Port-based: This approach does not use domain names, IP addresses to differentiate between different site content, but instead uses different TCP port numbers, so users need to specify a port number to access the different virtual host sites when they browse.
1. Domain-based virtual host
- Provide domain name resolution for virtual hosting
You can build your own DNS server for testing, and use the local hosts to resolve
- Prepare Web page documents for a virtual host
Prepare site directories and Web documents for each virtual web host
[Email protected]/]# mkdir-p/var/www/html//]# mkdir-p/var/www/html/" " >/var/www/html/bbs/" " >/var/www/html/blog/index.html
- Add a virtual host configuration
[Email protected]/]# vim/usr/local/httpd/conf/extra/httpd-vhosts.conf//Configure a separate configuration fileNamevirtualhost *: the //set the virtual host listener address<directory"/var/www/html">//Set Directory access permissionsOrder Allow,deny allow from All</directory><virtualhost *: the>DocumentRoot"/var/www/html/bbs"ServerName bbs.httpd.com errorlog"Logs/bbs.httpd.com.error_log"Customlog"Logs/bbs.httpd.com.access_log"Common</virtualhost><virtualhost *: the>DocumentRoot"/var/www/html/blog"ServerName blog.httpd.com errorlog"Logs/blog.httpd.com.error_log"Customlog"Logs/dummy-host2.example.com.access_log"Common</VirtualHost>[[Email protected]/]# vim/usr/local/httpd/conf/Httpd.confinclude conf/extra/httpd-vhosts.conf//load stand-alone configuration files
[[email protected]/]#/usr/local/httpd/bin/apachectl restart //Restart service make configuration effective
[Email protected]/]# vim/etc/hosts192.168. 119.131 bbs.httpd.com192.168. 119.131 Blog.httpd.com
- Accessing the virtual Web host in the client
[Email protected]/]# Curl bbs.httpd.com/]# Curl Blog.httpd.com
2. IP address-based virtual host
When you configure a virtual Web host that is based on an IP address, you do not use the Namevirtualhost configuration item to specify the IP address of the listening service
[Email protected]/]# vim/usr/local/httpd/conf/extra/httpd-vhosts.conf//Configure a separate configuration file<directory"/var/www/html">//Set Directory access permissionsOrder Allow,deny allow from All</directory><virtualhost192.168.119.131: the>DocumentRoot"/var/www/html/bbs"ServerName bbs.httpd.com errorlog"Logs/bbs.httpd.com.error_log"Customlog"Logs/bbs.httpd.com.access_log"Common</virtualhost><virtualhost192.168.119.132: the>DocumentRoot"/var/www/html/blog"ServerName blog.httpd.com errorlog"Logs/blog.httpd.com.error_log"Customlog"Logs/dummy-host2.example.com.access_log"Common</VirtualHost>[[Email protected]/]# vim/usr/local/httpd/conf/Httpd.confinclude conf/extra/httpd-vhosts.conf//load stand-alone configuration files[Email protected]/]#/usr/local/httpd/bin/apachectl restart//Restart the service for configuration to take effect
3. Port-based virtual host
When you configure a port-based virtual Web host, you do not use the Namevirtualhost configuration item to specify the IP address of the listening service
[Email protected]/]# vim/usr/local/httpd/conf/extra/httpd-vhosts.conf//Configure a separate configuration file<directory"/var/www/html">//Set Directory access permissionsOrder Allow,deny allow from All</directory><virtualhost192.168.119.131: the>DocumentRoot"/var/www/html/bbs"ServerName bbs.httpd.com errorlog"Logs/bbs.httpd.com.error_log"Customlog"Logs/bbs.httpd.com.access_log"Common</virtualhost><virtualhost192.168.119.131: the>DocumentRoot"/var/www/html/blog"ServerName blog.httpd.com errorlog"Logs/blog.httpd.com.error_log"Customlog"Logs/dummy-host2.example.com.access_log"Common</VirtualHost>[[Email protected]/]# vim/usr/local/httpd/conf/Httpd.confinclude conf/extra/httpd-vhosts.conf//load stand-alone configuration filesListen192.168.119.131:80//Monitor 80 portListen192.168.119.131:8080//monitor 8080 port[[Email protected]/]#/usr/local/httpd/bin/apachectl Restart//Restart the service for configuration to take effect
Apache Virtual Host Namevirtualhost