Apache 支援「基於 IP」與「基於域名」兩種虛擬主機模式。本文針對基於 IP 的虛擬主機,說明 Windows 與 Linux 下的完整配置步驟,並探討高可用架構中虛擬 IP(VIP)的角色。
在 httpd-vhosts.conf 中定義兩個不同 IP 的虛擬主機:
<VirtualHost 127.0.0.1:80>
DocumentRoot "C:/xampp/htdocs/yiyunmap"
ServerName 127.0.0.1:80
</VirtualHost>
<VirtualHost 127.0.0.2:80>
DocumentRoot "C:/xampp/htdocs/user"
ServerName 127.0.0.2:80
</VirtualHost>
<Directory "C:/xampp/htdocs/yiyunmap">
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Require all granted
</Directory>
<Directory "C:/xampp/htdocs/user">
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Require all granted
</Directory>
需在系統網卡綁定 127.0.0.2 別名(Windows 可用 netsh interface ip add address 或圖形介面新增)。
ifconfig eth0 192.168.149.1 netmask 255.255.255.0 up ifconfig eth0:1 192.168.149.2 netmask 255.255.255.0 up永久生效請修改
/etc/sysconfig/network-scripts/ifcfg-eth0:1。
/etc/httpd/conf/httpd.conf 或 conf.d/vhosts.conf):
<VirtualHost 192.168.149.1>
ServerAdmin root@abc.com
DocumentRoot /www/abc
ServerName www.abc.com
</VirtualHost>
<VirtualHost 192.168.149.2>
ServerAdmin root@good.com
DocumentRoot /www/good
ServerName www.good.com
</VirtualHost>
echo 'Welcome to abc' > /www/abc/index.html echo 'Welcome to GOOD' > /www/good/index.html
service httpd restart單點 Apache 宕機會導致整個叢集不可用。生產環境常採用 LVS (Linux Virtual Server) + Keepalived / Heartbeat 方案:
架構圖:Client → VIP (Keepalived) → LVS → Apache 叢集
mod_proxy_balancer 屬應用層轉發,效能與健檢能力遠弱於 LVS,適合中小流量;大併發必須上 LVS/Nginx L4。正確規劃虛擬 IP 與虛擬主機,是構建可擴展、高可用 Web 基礎設施的關鍵一步。