Apache ProxyPass 透過動態主機名

Apache ProxyPass 透過動態主機名

我正在嘗試將 Apache 配置為基於動態主機名稱並使用其他連接埠代理到內部伺服器。對我來說似乎有點太複雜了。這就是我所擁有的:

<Location /awesomewebapp>
ProxyPass http://[internalservername]:8080/awesomewebapp
ProxyPassReverse http://[internalservername]:8080/awesomewebapp
</Location>

但這(顯然)不是動態的。我一直在研究重寫規則等,但未能使其發揮作用。我真正需要的是這樣的:

^/[internalservername]/awesomewebapp
      proxy internally to
http://[internalservername]:8080/awesomewebapp

先致謝

答案1

您可以使用 RewriteRule 來代理請求。這使用 mod_rewrite 作為代理的 apache 文檔顯示以下內容:

描述:

mod_rewrite 提供 [P] 標誌,允許透過 mod_proxy 將 URL 傳遞到另一台伺服器。這裡給兩個例子。在一個範例中,URL 直接傳遞到另一台伺服器,並像本機 URL 一樣提供服務。在另一個範例中,我們將遺失的內容代理到後端伺服器。

解決方案:

為了簡單地將 URL 對應到另一個伺服器,我們使用 [P] 標誌,如下所示:

RewriteBase 上的 RewriteEngine“/products/”RewriteRule“^widget/(.*)$”“http://product.example.com/widget/$1" [P] ProxyPassReverse "/products/widget/" "http://product.example.com/widget/

在第二個範例中,只有當我們在本地找不到資源時,我們才代理請求。當您從一台伺服器遷移到另一台伺服器並且不確定所有內容是否已遷移時,這可能非常有用。

RewriteCond "%{REQUEST_FILENAME}" !-f RewriteCond "%{REQUEST_FILENAME}" !-d RewriteRule "^/(.*)" "http://old.example.com/$1" [P] ProxyPassReverse "/" "http://old.example.com/

第二個範例,使用 [P] 標誌有條件地重寫 URL 來代理請求,這似乎正是您所需要的。

答案2

我之前的“解決方案”仍然無法正常工作,因此我將其刪除以防止混淆。這似乎不可能按照我想要的方式進行,但是使用子域而不是子目錄的小解決方法仍然使它很穩定。

RewriteCond %{HTTP_HOST} ([^.]+)
RewriteRule ^/(.*) http://%1:8080/$1 [P]

來源:http://www.vanbommelonline.nl/2011/10/using-apache-for-dynamic-reverse-proxy.html

相關內容