1)基于 IP 的虚拟主机 2)具有相同端口的多个虚拟主机 3)使用宏定义虚拟主机(Apache2.4)
1)基于 IP 的虚拟主机
<VirtualHost 192.168.13.37>
ServerName example.com
DocumentRoot /var/www/domains/example.com/html
ErrorLog /var/log/example.com/error.log
CustomLog /var/log/example.com/access.log common
</VirtualHost>
<VirtualHost 192.168.47.11>
ServerName otherurl.com
DocumentRoot /srv/www/htdocs/otherurl.com/html
ErrorLog /var/log/otherurl.com/error.log
CustomLog /var/log/otherurl.com/access.log common
</VirtualHost>
只需将端口更改为你的给定 IP 即可。该端口与选择 vhost 的决定无关。
2)具有相同端口的多个 vhost
由于不再需要 NameVirtualHost,你可以使用相同的端口编写多个 vhost。
<VirtualHost *:80>
DocumentRoot /srv/www/htdocs/otherurl.com/html
ErrorLog /var/log/otherurl.com/error.log
CustomLog /var/log/otherurl.com/access.log common
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias ex1.com ex2.com
DocumentRoot /var/www/domains/example.com/html
ErrorLog /var/log/example.com/error.log
CustomLog /var/log/example.com/access.log common
</VirtualHost>
这里的情况恰恰相反:IP 无关紧要,但如果在端口 80 上收到请求,则会评估你输入的名称。你调用 ex1.com 第二个 vhost 被选中了吗?如果你调用任何其他网址(如 otherurl.com,还有 example3.com),将会选择第一个网址。如果愿意,你可以将此虚拟主机用作后备。
3)使用宏定义 vhost(Apache2.4)
<Macro VHost $port $host>
<VirtualHost *:$port>
Servername $host
DocumentRoot /srv/www/htdocs/$host
ErrorLog /var/log/$host/error.log
</VirtualHost>
</Macro>
Use VHost 80 example.com
Use VHost 443 secure_example.com
创建两个 vhost,一个用于端口 80,一个用于 443,并相应地设置使用的变量。