Fork me on GitHub

Sonarqube安装部署 在Apache时 踩过的一些坑

项目背景:

1.由于Sonarqube 在5.6之后已经不支持内置https 官方推荐用apache或者nginx作反向代理。

2. 项目访问地址是以第三方转发的形式,如实际部署地址是https://apacheserver:8999/  但是公司内部需要以https://adomain.com/sonarqube 访问,所以https://adomain.com/sonarqube被mapping到了 https://apacheserver:8999/  

 

 sonarqube是采用内置服务器,此处安装sonarqube略过,地址是http://xxxxxx:9568/sonarqube

 

所以我在apache 中的http-vhost.conf文件中设置了如下,请记得在httpd.conf中开启加载相关proxy module ,并注意设置cookie domain路径

 
<VirtualHost *:8999>
    ServerName xxxxx.com
    ErrorLog "logs/sonar.com-error.log"
    CustomLog "logs/sonar.com-access.log" common

    SSLEngine on
SSLCertificateFile "D:/Apache24/conf/xxx.crt"
SSLCertificateKeyFile "D:/Apache24/conf/xxx.key"
SSLCertificateChainFile "D:/Apache24/conf/xxx.cer"

ProxyRequests Off
ProxyPreserveHost On 
SSLProxyEngine on 

<Proxy *>
Order deny,allow
Allow from all
</Proxy>


RewriteRule "^/sonarqube/sonarqube/(.*)$" "http://127.0.0.1:9567/sonarqube/$1" [P]


ProxyPass "/" http://xxxx:9568/sonarqube/
ProxyPassReverse "/" http://xxxx:9568/sonarqube/

ProxyPassReverseCookieDomain "xxxxx.xx.xxx.com" "xx-xx.xxxx.com"
ProxyPassReverseCookiePath "/sonarqube/" "/"

</VirtualHost>

访问单独http地址 没问题:

http://xxxx:9568/sonarqube

但是访问 https://apacheserver:8999/  或者 https://adomain.com/sonarqube 就遇到了路径的问题,因为sonarqube 中很多路径地址是直接根目录地址"/"这就导致了很多文件的link是https://adomain.com/sonarqube/sonarqube/xxx.js这样的情况

多了一层目录这样是不正确的。正确的应该是https://adomain.com/sonarqube/xxx.js

所以此处只能在VirtualHost 重写地址

 

 

RewriteEngine On

RewriteRule "^/sonarqube/(.*)$"  "http://xxxx:9568/sonarqube/$1"  [P]

 另外 如果程序内部redirect 时  会出现丢https情况,比如8999是https端口,结果程序里 再redirect后 会以 http://xxxxxx:8999/  这样apache就会返回bad request 400错误。

解决方案 在httpd-vhosts.conf文件中 在<VirtualHost *:8999>节点之外添加如下内容,请记住不要在节点内添加。

 

RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "8999"

  

访问ok!

note: 关于 反向代理需要load 哪些module的请自行网上搜索。

 http://httpd.apache.org/docs/current/rewrite/proxy.html

posted @ 2018-01-19 14:30  低调的神  阅读(211)  评论(0编辑  收藏  举报