2011-12-16 194 views
95

我一直在努力這一段時間,我肯定做錯了什麼。Apache重定向到另一個端口

我在同一臺機器上有apache服務器和JBoss服務器。我想重定向mydomain.com的流量到JBoss localhost:8080/example。 DNS當前爲mydomain.com設置,並且在進入瀏覽器時將直接進入端口80。

我的問題是,當某個域名到達Apache(在本例中爲「mydomain.com」)時,我該如何重定向到不同的端口?

<VirtualHost ip.addr.is.here> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName mydomain.com 
    ProxyPass http://mydomain.com http://localhost:8080/example 
    ProxyPassReverse http://mydomain.com http://localhost:8080/example 
</VirtualHost> 

修訂W /建議 - 還沒轉發到端口8080

<VirtualHost *:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName mydomain.com 
    ServerAlias www.mydomain.com 
    ProxyPass http://mydomain.com http://localhost:8080/example 
    ProxyPassReverse http://mydomain.com http://localhost:8080/example 
</VirtualHost> 
+0

這看起來不錯。你的症狀是什麼? – 2011-12-16 23:08:04

+0

Sympotms是我把www.mydomain.com放在瀏覽器中,但它轉到apache根目錄。相反,我希望它重定向到一個jboss子目錄localhost:8080/subdir。我目前正在將各個域直接重定向到80端口,但無法將其轉移到另一個端口。 – agentcurry 2011-12-16 23:20:14

+0

我有完全相同的要求:您是否找到解決方案? – Cystack 2012-06-27 12:27:43

回答

138

您應該忽略ProxyPass和ProxyPassReverse中的域http://example.com並將其保留爲/。此外,您需要將/保留在example/的末尾以重定向到的位置。此外,我在http://example.comhttp://www.example.com之間遇到了一些問題 - 只有www在我製作了ServerName www.example.com和ServerAlias example.com之前一直工作。給以下一個去。

<VirtualHost *:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName www.example.com 
    ServerAlias example.com 
    ProxyPass/http://localhost:8080/example/ 
    ProxyPassReverse/http://localhost:8080/example/ 
</VirtualHost> 

後進行這些更改,添加需要的模塊,並重新啓動Apache

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart 
2

如果你沒有使用代理服務器JBoss和mydomain.com:8080可以「暴露「世界,然後我會這樣做。

<VirtualHost *:80> 
    ServerName mydomain.com 
    Redirect 301/http://mydomain.com:8080/ 
</VirtualHost> 
0

Apache支持基於名稱和基於IP的虛擬主機。看起來你正在使用兩者,這可能不是你所需要的。

我想你實際上是在設置name-based virtual hosting,因此你不需要指定IP地址。

嘗試< VirtualHost *:80>綁定到所有IP地址,除非您真的想要ip based virtual hosting。如果服務器有多個IP地址,並且您想在不同的地址上提供不同的站點,則可能會出現這種情況。最常見的設置是(我猜)基於名稱的虛擬主機。

0

您需要兩兩件事:

  1. 添加ServerAlias www.mydomain.com到你的配置
  2. 改變你的ProxyPass到ProxyPassMatch ^(.*)$ http://localhost:8080/example$1,對可能保持mod_dir和結尾的斜槓從interferring。
23

我解決了這個問題,用下面的代碼:

LoadModule proxy_module modules/mod_proxy.so 
LoadModule proxy_http_module modules/mod_proxy_http.so 
<VirtualHost *:80> 
ProxyPreserveHost On 
ProxyRequests Off 
ServerName myhost.com 
ServerAlias ww.myhost.com 
ProxyPass/http://localhost:8080/ 
ProxyPassReverse/http://localhost:8080/ 
</VirtualHost> 

我也用過:

a2enmod proxy_http 
7

我想d正是這個,所以我可以從根域訪問Jenkins。

我發現我必須禁用默認網站才能使其工作。這正是我所做的。

$ sudo vi /etc/apache2/sites-available/jenkins 

,並插入到這個文件:

<VirtualHost *:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName mydomain.com 
    ServerAlias mydomain 
    ProxyPass/http://localhost:8080/ 
    ProxyPassReverse/http://localhost:8080/ 
    <Proxy *> 
     Order deny,allow 
     Allow from all 
    </Proxy> 
</VirtualHost> 

接下來,您需要啓用/禁用相應的網站:

$ sudo a2ensite jenkins 
$ sudo a2dissite default 
$ sudo service apache2 reload 

希望它可以幫助別人。

2

這可能是一個老問題,但這裏是我所做的:

在.conf文件文件由apache加載:

<VirtualHost *:80> 
    ServerName something.com 
    ProxyPass/http://localhost:8080/ 
</VirtualHost> 

說明:監聽所有請求都發送到本地機器的端口80.如果我請求「http://something.com/somethingorother」,請將該請求轉發到「http://localhost:8080/somethingorother」。這應該適用於外部訪問者,因爲根據文檔,它將遠程請求映射到本地服務器的空間。

我正在運行Apache 2.4.6-2ubuntu2.2,所以我不確定「-2ubuntu2.2」如何影響此答案的更廣泛適用性。

後進行這些更改,添加需要的模塊,並重新啓動Apache

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart 
4

通過反覆試驗發現了這一點。如果你的配置指定了一個ServerName,那麼你的VirtualHost指令將需要做同樣的事情。在下面的例子中,awesome.example.com和amazing.example.com都將被轉發到一些本地服務運行在端口4567

ServerName example.com:80 

<VirtualHost example.com:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName awesome.example.com 
    ServerAlias amazing.example.com 
    ProxyPass/http://localhost:4567/ 
    ProxyPassReverse/http://localhost:4567/ 
</VirtualHost> 

我知道這並不完全回答這個問題,但我把它放在這裏是因爲這是Apache端口轉發的最高搜索結果。所以我認爲這有助於某個人。

3

您必須確保服務器上已啓用代理。您可以使用以下命令執行此操作:

a2enmod proxy 
    a2enmod proxy_http 

    service apache2 restart 
0

所有這些都是通過虛擬服務器上的域名訪問端口的絕佳見解。但是,不要忘記啓用虛擬服務器;這可以被註釋掉:

NameVirtualHost *:80 
<Directory "/home/dawba/www/"> 
allow from all 
</Directory> 

我們運行WSGI與域sxxxx.com並在端口6800上運行某些防火牆似乎阻止與端口個域名服務器golang Apache服務器。這是我們的解決方案:

<VirtualHost *:80> 
ProxyPreserveHost On 
ProxyRequests Off 
ServerName wsgi.sxxxx.com 
DocumentRoot "/home/dxxxx/www" 
    <Directory "/home/dxxx/www"> 
    Options Indexes FollowSymLinks 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
    </Directory> 
ScriptAlias /py/ "/home/dxxxx/www/py/" 
WSGIScriptAlias /wsgiprog /home/dxxxx/www/wsgiprog/Form/Start.wsgi 
</VirtualHost> 

<VirtualHost *:80> 
ProxyPreserveHost On 
ProxyRequests Off 
ServerName sxxxx.com 
ServerAlias www.sxxxx.com 
ProxyPass/http://localhost:6800/ 
ProxyPassReverse/http://localhost:6800/ 
</VirtualHost> 
0

我的Apache監聽2個不同的端口,

Listen 8080 
Listen 80 

我用的是80級的時候我希望有一個透明的網址,不要把該端口的URL 對谷歌有用的後不允許本地url的服務?

但是我用8080給內部開發,其中我使用的端口作爲參考的「開發環境」

0

這在ISPConfig工作壓力太大。在網站列表中進入一個域內,點擊選項標籤,添加這些行:;

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

然後去網站和wolaa :)這也是HTTPS協議。

相關問題