簡體   English   中英

Apache重定向到另一個端口

[英]Apache redirect to another port

我已經為此苦苦掙扎了一段時間,並且肯定做錯了什么。

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

我的問題是,當某個域名進入 Apache(在本例中為mydomain.example )時,我如何重定向到不同的端口?

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

實施一些建議后:

  • 仍然沒有轉發到端口 8080

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

您應該在 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

我用以下代碼解決了這個問題:

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

我也用過:

a2enmod proxy_http

我想這樣做,這樣我就可以從根域訪問 Jenkins。

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

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

並將其插入文件中:

<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName mydomain.example
  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

通過反復試驗發現了這一點。 如果您的配置指定了 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 端口轉發的熱門搜索結果。 所以我認為它總有一天會幫助某人。

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

  a2enmod proxy
  a2enmod proxy_http

  service apache2 restart

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

在 Apache 加載的 .conf 文件中:

<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 配置中使用反向代理(直接):

ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar

在此處查找有關如何使用 mod 的 apache 文檔

如果您不必使用 JBoss 的代理並且mydomain.example:8080可以“公開”給世界,那么我會這樣做。

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

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

Listen 8080
Listen 80  

當我想要一個透明的 URL 並且不將端口放在對不允許本地 URL 的谷歌服務有用的 URL 之后,我使用 80?

但我使用 8080 進行內部開發,我使用端口作為“開發環境”的參考

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

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

我們使用域 sxxxx.com 上的 Apache 服務器和運行在端口 6800 上的 golang 服務器運行 WSGI。一些防火牆似乎阻止了帶有端口的域名。 這是我們的解決方案:

<VirtualHost *:80>
 ProxyPreserveHost On
 ProxyRequests Off
 ServerName wsgi.sxxxx.example
 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>

這也適用於 ISPConfig。 在網站列表中進入域,單擊選項選項卡,添加以下行:

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

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

試試這個——

<VirtualHost *:80> 
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName www.adminbackend.example.com
  ServerAlias adminbackend.example.com
  ProxyPass / http://localhost:6000/
  ProxyPassReverse / http://localhost:6000/
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> 

這就是我將部分請求重定向到一個 url 並休息到另一個 url 的方式:

<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName localhost
  ProxyPass /context/static/content http://localhost:80/web/
  ProxyPassReverse /context/static/content http://localhost:80/web/
  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
</VirtualHost>

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

我認為您實際上是在嘗試設置基於名稱的虛擬主機,為此您無需指定 IP 地址。

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

你需要兩件事:

  1. ServerAlias www.mydomain.example添加到您的配置中
  2. 將您的 proxypass 更改為ProxyPassMatch ^(.*)$ http://localhost:8080/example$1 ,以防止 mod_dir 和尾部斜杠干擾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM