Easily configure httpd virtual hosts and httpd Virtual Hosts
Httpd uses the VirtualHost command to define a virtual host. Three VM types are supported: ip-based, Port-based, and name-based. Port-based virtual hosts are also IP-based in terms of httpd (such as the official manual.
When a request arrives, it matches the VM first. Matching the VM rules is the best match method. The best practice is that the smaller the number of available configurations, the higher the matching priority. For example, "192.168.100.14: 80" has a higher priority than "*: 80 ". If the name-based VM cannot match, use the first VM in the VM list as the response host. If none of the VM instances match, use the host in the master configuration section. If DocumentRoot is commented in the master configuration section, the corresponding error is returned.
Commands of the main configuration section can basically be used in the VM container. There is no rule as to what commands must be included in the VM, because the VM only encapsulates a group of commands, even if there are no commands, it will inherit from the main configuration section. However, since virtual hosts are to be used and used, generally speaking, at least different ServerName, DocumentRoot and other commands must be provided to separate them.
It should be noted that the httpd "-S" option is very useful when debugging virtual host configuration options.
1. IP-based VM
IP-based virtual hosts provide different site services on different IP addresses and ports, and provide different sites on different ports.
If you use different IP addresses only based on IP addresses, you must have two or more IP addresses on the operating system. You can provide multiple NICs or use the NIC alias.
If you use different ports based on the port, you can use the same IP address or different IP addresses. However, in httpd terminology, a VM is based on a single IP address but different ports, it is also an IP-based virtual host.
Assume that the local machine is 192.168.100.14.
# First set a virtual network card. Shell> ip a add 192.168.100.144 dev eth0 label eth0: 0 # add an ip address-based VM. The relative path used by DocumentRoot is based on ServerRootshell> vim/etc/apache/extra/vhosts. conf <VirtualHost 192.168.100.14: 80> ServerName www.a.com DocumentRoot htdocs/a.com </VirtaulHost> <VirtualHost 192.168.100.144: 80> ServerName www. B .com DocumentRoot htdocs/B .com </VirtaulHost>
In the master configuration file, include the virtual host configuration file vhosts. conf.
include /etc/apache/extra/vhosts.conf
Then upload the documentrootfile and its own index.html file.
mkdir /usr/local/apache/htdocs/{a.com,b.com}echo '
Usehttpd -SView the configuration file loading process.
[root@xuexi httpd-2.4.27]# httpd -S -f /etc/apache/httpd.conf VirtualHost configuration:192.168.100.14:80 www.a.com (/etc/apache/extra/vhosts.conf:23)192.168.100.144:80 www.b.com (/etc/apache/extra/vhosts.conf:28)ServerRoot: "/usr/local/apache"Main DocumentRoot: "/usr/local/apache/htdocs"Main ErrorLog: "/usr/local/apache/logs/error_log"Mutex proxy: using_defaultsMutex default: dir="/usr/local/apache/logs/" mechanism=default PidFile: "/usr/local/apache/logs/httpd.pid"Define: DUMP_VHOSTSDefine: DUMP_RUN_CFGUser: name="daemon" id=2Group: name="daemon" id=2
Restart httpd.
service httpd restart
Test.
2. Port-based VM Port-based virtual hosts need to listen to two sockets.
First, use the Listen command in the configuration file to modify the listening socket. Assume that it is only based on the port, so you only need to modify the port number.
listen 80listen 8080
Modify the virtual host configuration file vhosts. conf as follows:
shell> vim /etc/apache/extra/vhosts.conf<VirtualHost 192.168.100.14:80> ServerName www.a.com DocumentRoot htdocs/a.com</VirtaulHost><VirtualHost 192.168.100.14:8080> ServerName www.b.com DocumentRoot htdocs/b.com</VirtaulHost>
Restart httpd. Test whether www.a.com and www. B .com are displayed.
3. Name-based VM The request message contains two resource locating formats: TCP/IP and HTTP. Although the TCP/IP is the same, however, the HOST is specified in the HTTP request message, which is the reason why the domain name-based virtual HOST can be implemented. Therefore, the name-based VM must specify the ServerName command, otherwise it will inherit the FQDN of the operating system.
shell> vim /etc/apache/extra/vhosts.conf<VirtualHost 192.168.100.14:80> ServerName www.a.com DocumentRoot htdocs/a.com</VirtaulHost><VirtualHost 192.168.100.14:80> ServerName www.b.com DocumentRoot htdocs/b.com</VirtaulHost>
Note: For a name-based Vm, when an IP address request (for example, an IP address is entered in a browser) or cannot match any Vm, the first virtual host will be used as the default virtual host.
For example, if "192.168.100.14 www.c.com" is added to a hosts file, however, the first VM in the VM list is still accessed.
Go back to the Linux series article outline: workshop!