Apache 重定向到另一个端口

标签 apache redirect

我已经为此苦苦挣扎了一段时间,而且我肯定做错了什么。

我在同一台机器上有 Apache 服务器和 JBoss 服务器。我想将 mydomain.example 的流量重定向到 JBoss localhost:8080/example。当前已为 mydomain.example 设置 DNS,在浏览器中输入时它将直接访问端口 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 和服务器别名 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

关于Apache 重定向到另一个端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8541182/

相关文章:

HTTPS 中的 Apache 重定向

iOS - 加载不工作

java - 如何在 servlet 过滤器中重定向?

php - 一个关于mysql select查询的问题!

apache - 日志记录不适用于其他 tomcat 实例

php - "413 Request Entity Too Large"用于 AJAX POST 请求但不用于普通文件上传

c - Apache 2.4 MPM worker : Thread Init?

java - 从移动设备指向 ebay.com 时出现 fileNotFoundException

CakePHP 登录重定向到请求的 URL

.htaccess - 通过 .htaccess 将 URL 重定向到自定义 URL