Apache 反向代理配置

Apache 反向代理配置

所以我有一個運行 Apache 2.4.25 的 Debian 9 機器

我希望 Apache 也能從另一台伺服器提供 Web 內容(http://192.168.1.100:8088) 在網路上;我已經設定了 mod_proxy 來執行此操作,但很難讓事情正常工作。

此配置似乎幾乎可以正常工作,因為我從正確的伺服器獲取了一些錯誤內容,似乎存在 url 問題,app1 端點可能附加到 url 上?該端點實際上並不存在於任何地方,僅用於捕獲請求。

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    ProxyPreserveHost On
    ProxyRequests Off
    <proxy *>
      Order deny,allow
      Allow from all
    </proxy>
    ProxyPass /app1 http://192.168.1.100:8088/
    ProxyPassReverse /app1 http://localhost/
</virtualHost>

如果我嘗試像這樣代理所有請求,它工作正常,但顯然我無法存取本地主機上的任何資源。

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    ProxyPreserveHost On
    ProxyRequests Off
    <proxy *>
      Order deny,allow
      Allow from all
    </proxy>
    ProxyPass / http://192.168.1.100:8088/
    ProxyPassReverse / http://localhost/
</virtualHost>

我想也許我需要執行某種重寫?我對使用 Apache mod_proxy 並不是特別有經驗。

:-(

答案1

正如評論中提到的,您至少有兩個明顯的問題:

  1. ProxyPass每個個體和語句中的「終止」斜線ProxyPassReverse需要配對。

  2. ProxyPass並且ProxyPassReverse需要引用同一主機。

所以,在你的第一個例子:

ProxyPass /app1 http://192.168.1.100:8088/
ProxyPassReverse /app1 http://localhost/

應該:

ProxyPass /app1 http://192.168.1.100:8088
ProxyPassReverse /app1 http://192.168.1.100:8088

或者:

ProxyPass /app1 http://localhost
ProxyPassReverse /app1 http://localhost

對於第二個範例,/出於此處提到的目的,被視為「終止」斜線。所以:

ProxyPass / http://192.168.1.100:8088/
ProxyPassReverse / http://localhost/

只需要匹配主機即可:

ProxyPass / http://192.168.1.100:8088/
ProxyPassReverse / http://192.168.1.100:8088/

或者:

ProxyPass / http://localhost/
ProxyPassReverse / http://localhost/

筆記

  • 可能值得一提的是,Apache 處理它代理的 URL 的方式可能比依賴接收應用程式如何建構自己的 URL/獲得更好的結果。/app1

  • 您可能需要代理多個 URL,具體取決於接收應用程式的工作方式。

  • 如果由於某種原因尚未啟用它們,您可能希望啟用它們mod_proxy_htmlmod_proxy_httpmod_proxy

相關內容