我正在嘗試在新的 Ubuntu 22.04 虛擬機器上將 Apache 設定為反向代理。我們在 Ubuntu 18.04 上有一個現有的 Apache 反向代理,一切都按預期運行。這個新的反向代理應該取代我們的舊反向代理,但虛擬主機似乎無法正常運作。
我做了以下事情:
apt-get update
apt-get upgrade
apt-get install apache2
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_balancer
a2enmod lbmethod_byrequests
我禁用了啟用網站中的預設頁面。
a2dissite 000-default.conf
然後我創建了一個新的虛擬主機並啟用它。
vi 001-trupage.azmedien.ch.conf
a2ensite 001-trupage.azmedien.ch.conf
它看起來像這樣:
<VirtualHost trupage.azmedien.ch:80>
ServerName trupage.azmedien.ch
ProxyPreserveHost On
ProxyPass / http://10.200.0.130/
ProxyPassReverse / http://10.200.0.130/
</VirtualHost>
然後我重新啟動並重新載入 Apache。
systemctl restart apache2
systemctl reload apache2
我在 Windows PC 上建立了一個主機檔案條目來測試這個非常基本的配置是否有效,它指向我的 Apache 伺服器。
當我嘗試存取 trupage.azmedien.ch 時,它會將我引導至 Apache 預設網站,而不是實際將我重新導向到正確的伺服器(在虛擬主機中使用 ProxyPass 定義)。
似乎由於某些原因它無法辨識虛擬主機?當我將“trupage.azmedien.ch:80”替換為“*:80”,然後在瀏覽器中開啟它時,ProxyPass 就可以工作了。但顯然,這不是我想要的,因為會有多個虛擬主機。
<VirtualHost *:80>
ServerName trupage.azmedien.ch
ProxyPreserveHost On
ProxyPass / http://10.200.0.130/
ProxyPassReverse / http://10.200.0.130/
</VirtualHost>
這是 的輸出apache2ctl -S
,213.146.11.131 是舊反向代理的 IP,但我不知道它來自哪裡或為什麼會出現在這裡:
root@azprox10:~# apache2ctl -S
AH00558: apache2: Could not reliably determine the server's fully qualified doma in name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress th is message
VirtualHost configuration:
213.146.11.131:80 is a NameVirtualHost
default server localhost (/etc/apache2/sites-enabled/001-trupage.azmedi
en.ch.conf:1)
port 80 namevhost localhost (/etc/apache2/sites-enabled/001-trupage.azm
edien.ch.conf:1)
port 80 namevhost opvsg.chmedia.ch (/etc/apache2/sites-enabled/002-opvs
g.chmedia.ch.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex proxy: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
答案1
您不需要該<VirtualHost>
行中的 dns 名稱。<VirtualHost *:80>
很好,您可以有多個,相關的是ServerName
指令。
該行中的項目<VirtualHost>
僅定義 Apache 應偵聽的介面。如果您在此輸入主機名,Apache 會嘗試將其解析為 IP 位址以找到正確的介面。