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,並相應地設定使用的變數。