我已经为此苦苦挣扎了一段时间,而且我肯定做错了什么。
我在同一台机器上有 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.com
与 http://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/