Two reverse proxies on the same domain, different path.

huangapple go评论52阅读模式
英文:

Two reverse proxies on the same domain, diferent path

问题

我正在尝试在同一台机器上配置两个不同的反向代理,使用不同的路径。它们被设置为代理 SonarQube(localhost:9000/sonar)和 Jenkins(localhost:8080/jenkins)。

我正在尝试实现这样的效果:访问 http://server.name/sonar 时,将代理到 http://localhost:9000/sonar。

我有两个问题:

  1. Sonar 被正确代理,但在回调 URL(点击标志时)会指向 http://server.name,而不是 http://server.name/sonar。我已尝试使用 X-Forwarded-Host 标头(应该也包含在 ProxyPreserveHost 指令中)。

  2. Jenkins 代理不起作用。它返回 404 错误。

我做错了什么?

ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
  ServerName server.name/sonar
  ProxyPass /sonar http://localhost:9000/sonar
  ProxyPassReverse /sonar http://localhost:9000/sonar
  ErrorLog logs/sonar/error.log
  CustomLog logs/sonar/access.log common
</VirtualHost>

# VIRTUAL HOSTS
####################
<VirtualHost *:80>
  ServerName server.name/jenkins
  ProxyPass /jenkins http://localhost:8080/jenkins
  ProxyPassReverse /jenkins http://localhost:8080/jenkins
  ErrorLog logs/jenkins/error.log
  CustomLog logs/jenkins/access.log common
</VirtualHost>
英文:

I'm trying to configure two different reverse proxies on the same machine with diferent paths. They are set to proxy SonarQube (localhost:9000/sonar) and Jenkins (localhost:8080/jenkins).

I'm trying to achive that calling to http://server.name/sonar, proxifys to http://localhost:9000/sonar.

I have two issues:

  • Sonar is correctly proxified but on the callback url (clicking in the logo), will point to http://server.name, instead http://server.name/sonar. I have tried with the X-Forwarded-Host header (which should be also included in ProxyPreserveHost directive).
  • Jenkins proxy is not working. It crashes with a 404.

What I'm doing wrong?

ProxyRequests Off
ProxyPreserveHost On
&lt;VirtualHost *:80&gt;
  ServerName server.name/sonar
  ProxyPass /sonar http://localhost:9000/sonar
  ProxyPassReverse /sonar http://localhost:9000/sonar
  ErrorLog logs/sonar/error.log
  CustomLog logs/sonar/access.log common
&lt;/VirtualHost&gt;

# VIRTUAL HOSTS
####################
&lt;VirtualHost *:80&gt;
  ServerName server.name/jenkins
  ProxyPass /jenkins http://localhost:8080/jenkins
  ProxyPassReverse /jenkins http://localhost:8080/jenkins
  ErrorLog logs/jenkins/error.log
  CustomLog logs/jenkins/access.log common
&lt;/VirtualHost&gt;

答案1

得分: 2

问题出在您定义虚拟主机的方式上。ServerName 只能理解域名,而不能理解 /something。

选项1 定义域名前缀。

<VirtualHost *:80>
    ServerName sonar.server.name
    
    ProxyPass /sonar http://localhost:9000/sonar/
    ProxyPassReverse /sonar http://localhost:9000/sonar/
    
    ErrorLog logs/sonar/error.log
    CustomLog logs/sonar/access.log common
</VirtualHost>

<VirtualHost *:80>
    ServerName jenkins.server.name
    
    ProxyPass /jenkins http://localhost:8080/jenkins/
    ProxyPassReverse /jenkins http://localhost:8080/jenkins/
    
    ErrorLog logs/jenkins/error.log
    CustomLog logs/jenkins/access.log common
</VirtualHost>

然后,您需要使用 http://jenkins.server.name/jenkins/http://sonar.server.name/sonar/

选项2 与选项1类似,但不再需要 /sonar 或 /jenkins。

<VirtualHost *:80>
    ServerName sonar.server.name
    
    ProxyPass / http://localhost:9000/sonar/
    ProxyPassReverse / http://localhost:9000/sonar/
    
    ErrorLog logs/sonar/error.log
    CustomLog logs/sonar/access.log common
</VirtualHost>

<VirtualHost *:80>
    ServerName jenkins.server.name
    
    ProxyPass / http://localhost:8080/jenkins/
    ProxyPassReverse / http://localhost:8080/jenkins/
    
    ErrorLog logs/jenkins/error.log
    CustomLog logs/jenkins/access.log common
</VirtualHost>

在这里,使用 http://jenkins.server.name/http://sonar.server.name/

选项3 保留一个域名,没有前缀,并在 /something 上分割。

全部放在一个虚拟主机中,像这样:

<VirtualHost *:80>
    ServerName server.name
    
    ProxyPass /sonar http://localhost:9000/sonar/
    ProxyPassReverse /sonar http://localhost:9000/sonar/

    ProxyPass /jenkins http://localhost:8080/jenkins/
    ProxyPassReverse /jenkins http://localhost:8080/jenkins/
    
    ErrorLog logs/error.log
    CustomLog logs/access.log common
</VirtualHost>

不幸的是,以这种方式做,您会失去单独的日志文件。CustomLog 只适用于全局服务器配置或虚拟主机内部。

使用 http://server.name/jenkins/http://server.name/sonar/

最后确保加载 mod_proxy_html 模块以及其他代理模块。

英文:

The problem is in the way you define your Virtual Hosts. ServerName does not understand /something, only domain names.

Option 1 define domain prefixes.

&lt;VirtualHost *:80&gt;
    ServerName sonar.server.name
    
    ProxyPass /sonar http://localhost:9000/sonar/
    ProxyPassReverse /sonar http://localhost:9000/sonar/
    
    ErrorLog logs/sonar/error.log
    CustomLog logs/sonar/access.log common
&lt;/VirtualHost&gt;

&lt;VirtualHost *:80&gt;
    ServerName jenkins.server.name
    
    ProxyPass /jenkins http://localhost:8080/jenkins/
    ProxyPassReverse /jenkins http://localhost:8080/jenkins/
    
    ErrorLog logs/jenkins/error.log
    CustomLog logs/jenkins/access.log common
&lt;/VirtualHost&gt;

You would then have to use http://jenkins.server.name/jenkins/ or http://sonar.server.name/sonar/.


Option 2 like option 1, but without the /sonar or /jenkins which are not required anymore.

&lt;VirtualHost *:80&gt;
    ServerName sonar.server.name
    
    ProxyPass / http://localhost:9000/sonar/
    ProxyPassReverse / http://localhost:9000/sonar/
    
    ErrorLog logs/sonar/error.log
    CustomLog logs/sonar/access.log common
&lt;/VirtualHost&gt;

&lt;VirtualHost *:80&gt;
    ServerName jenkins.server.name
    
    ProxyPass / http://localhost:8080/jenkins/
    ProxyPassReverse / http://localhost:8080/jenkins/
    
    ErrorLog logs/jenkins/error.log
    CustomLog logs/jenkins/access.log common
&lt;/VirtualHost&gt;

Here, use http://jenkins.server.name/ or http://sonar.server.name/.


Option 3 Keep one domain, no prefix and split on the /something

All in one Virtual Host. Like this:

&lt;VirtualHost *:80&gt;
    ServerName server.name
    
    ProxyPass /sonar http://localhost:9000/sonar/
    ProxyPassReverse /sonar http://localhost:9000/sonar/

    ProxyPass /jenkins http://localhost:8080/jenkins/
    ProxyPassReverse /jenkins http://localhost:8080/jenkins/
    
    ErrorLog logs/error.log
    CustomLog logs/access.log common
&lt;/VirtualHost&gt;

Sadly doing it this way you loose the separate log files. CustomLog is only applicable in the global server configuration or inside a VirtualHost.

Use http://server.name/jenkins/ or http://server.name/sonar/.


Finally ensure you load module mod_proxy_html along with the other proxy modules.

huangapple
  • 本文由 发表于 2023年5月25日 18:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定