apache 同一域上的两个反向代理,不同路径

tjjdgumg  于 2023-05-29  发布在  Apache
关注(0)|答案(1)|浏览(261)

我试图配置两个不同的反向代理在同一台机器上的不同路径。它们被设置为代理SonarQube(localhost:9000/sonar)和Jenkins(localhost:8080/jenkins)。
我正在尝试实现调用http://server.name/sonar,代理http://localhost:9000/sonar。
我有两个问题:

  • Sonar被正确地代理,但是在回调URL上(点击徽标),将指向http://server.name,而不是http://server.name/sonar。我已经尝试使用X-Forwarded-Host头(它也应该包含在ProxyPreserveHost指令中)。
  • 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>
exdqitrt

exdqitrt1#

问题在于您定义虚拟主机的方式。ServerName不懂/什么的,只懂域名。

选项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仅适用于全局服务器配置或VirtualHost内部。
使用http://server.name/jenkins/http://server.name/sonar/
最后,确保您加载模块mod_proxy_html沿着其他代理模块。

相关问题