如何将带有端口的URL重定向到带有上下文的URL?

时间:2019-03-27 06:51:32

标签: apache apache2

我有一台安装了 apache(2.4.18)的服务器

我已在服务器上安装了多个应用程序,例如Grafana,Sonarqube和MySQL企业监视器(MEM)

每个应用程序都具有这样的URL

http://test.com:9000

http://test.com:3000

我正在寻找一种解决方案,允许我将带有端口的URL重定向到具有上下文的URL

http://test.com:9000  --> http://test.com/sonar

http://test.com:3000 --> http://test.com/grafana

我已经在 /etc/apache2/sites-enabled/000-default.conf 文件中添加了一些代码

Redirect permanent /sonar http://test.com:9000

Redirect permanent /grafana http://test.com:3000

但是当我在网络浏览器中输入http://test.com/sonar时,它仅重定向到http://test.com:9000 URL

我希望http://test.com/sonar此URL在Web浏览器上保留

2 个答案:

答案 0 :(得分:0)

您需要代理请求,而不是重定向请求。 使用official apache proxy documentation

中提到的ProxyPass指令

例如,在配置中添加以下位置块:

$entity

答案 1 :(得分:0)

如果您使用Redirect permanent,则服务器会将301响应发送给客户端(以及新的Location)。这将导致浏览器发出新的请求,这次是向新的位置发出请求,并且新的位置也将显示在浏览器地址栏中。 您需要的是反向代理。为此,您需要确保在您的apache配置中启用了mod_proxy(通常默认情况下已启用),并将这样的内容放入您的.conf文件中:

ProxyPreserveHost On
ProxyPass        /sonar   http://127.0.0.1:9000
ProxyPassReverse /sonar   http://127.0.0.1:9000
ProxyPass        /grafana http://127.0.0.1:3000
ProxyPassReverse /grafana http://127.0.0.1:3000

您可能还必须使应用程序知道它们在非root上下文下运行(通过进行一些配置更改):

http://docs.grafana.org/installation/behind_proxy/

https://docs.sonarqube.org/latest/setup/install-server/