用于 Jenkins 和 Sonar 的带有 SSL 的 Apache 反向代理配置

fri*_*mle 12 ssl ssl-certificate jenkins apache-2.2 apache-2.4

我在 Apache 服务器后面运行两个服务:Jenkins(端口 8080)和 SonarQube(端口 9000)。

我的 apache 配置如下所示:

<VirtualHost *:80>
  ServerName server
  Redirect permanent / https://server.domain.com/
</VirtualHost>

<VirtualHost *:80>
  ServerName server.domain.com
  Redirect permanent / https://server.domain.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName server.domain.com

  SSLEngine on
  SSLCertificateFile /etc/ssl/certs/server.crt
  SSLCertificateKeyFile /etc/ssl/private/server.key

  ProxyPass        /jenkins http://localhost:8080/jenkins nocanon
  ProxyPassReverse /jenkins http://localhost:8080/jenkins
  ProxyPassReverse /jenkins http://server.domain.com/jenkins
  ProxyPassReverse /jenkins https://server.domain.com/jenkins

  ProxyPass        /sonar http://localhost:9000/sonar nocanon
  ProxyPassReverse /sonar http://localhost:9000/sonar

  AllowEncodedSlashes NoDecode
  ProxyRequests Off
  ProxyPreserveHost On
  <Proxy http://localhost:8080/*>
    Order deny,allow
    Allow from all
  </Proxy>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

一切似乎都运行良好,除了 Jenkins 抱怨此消息:您的反向代理设置似乎已损坏。

当我运行Jenkins 提供的ReverseProxySetupMonitor测试时,错误消息表明反向代理设置不正确,因为它没有用 https 替换 http:

$ curl -iLk -e https://server.domain.com/jenkins/manage https://server.domain.com/jenkins/administrativeMonitor/hudson.diagnosis.ReverseProxySetupMonitor/test
[...]
404 http://server.domain.com/jenkins/manage vs. https://server.domain.com/jenkins/manage
[...]
Run Code Online (Sandbox Code Playgroud)

这仅我在服务器上启用 SSL(现在使用自签名证书)后出现。

问题: 如何修复反向代理设置以使 Jenkins 满意?关于如何改进 apache 配置文件的提示的加分点。

我已经检查了以下两个相关问题:

mas*_*oeh 11

维基 Jenkins 上的这个页面提到,根据2014 年 7 月,Jenkins 反向代理的推荐配置。缺少的参数是RequestHeader set X-Forwarded-Proto "https"RequestHeader set X-Forwarded-Port "443"

所以配置变成了

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/cert.pem
    ServerAdmin  webmaster@localhost
    ProxyRequests     Off
    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass         /  http://localhost:8080/ nocanon
    ProxyPassReverse  /  http://localhost:8080/
    ProxyPassReverse  /  http://www.example.com/
    RequestHeader set X-Forwarded-Proto "https"
    RequestHeader set X-Forwarded-Port "443"
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

  • 太棒了,这完美地工作!我还必须做`sudo a2enmod headers`,否则我会得到`Invalid command 'RequestHeader'` (2认同)