【问题标题】:Jenkins behind nginx reverse proxy doesn't redirect in all situationsnginx反向代理背后的詹金斯在所有情况下都不会重定向
【发布时间】:2020-04-09 17:55:23
【问题描述】:

我有 jenkins 和 nginx 通过 docker-compose 运行,它们都在同一个 docker 网络上。 Jenkins 不向主机公开任何端口,其默认配置为在端口 8080 上运行,nginx 映射 8003:443

我们有一个位于专用网络和子域上的服务器,我有以下 nginx 配置文件

upstream jenkins {
  server        jenkins:8080;
}

server {
  listen   443 ssl;
  server_name   abc.example.com;
  ssl_certificate       /etc/ssl/private/certificate.crt;
  ssl_certificate_key   /etc/ssl/private/key.pem;

  root            /var/run/jenkins/war/;

  ignore_invalid_headers off; #pass through headers from Jenkins which are considered invalid by Nginx server.

  location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
    #rewrite all static files into requests to the root
    #E.g /static/12345678/css/something.css will become /css/something.css
    rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
  }

  location /userContent {
    #have nginx handle all the static requests to the userContent folder files
    #note : This is the $JENKINS_HOME dir
        root /var/lib/jenkins/;
    if (!-f $request_filename){
      #this file does not exist, might be a directory or a /**view** url
      rewrite (.*) /$1 last;
          break;
    }
        sendfile on;
  }


  location / {
    proxy_pass  http://jenkins/;
    proxy_buffering off;
    proxy_set_header X-Real-IP $remote_addr;
      sendfile off;
      proxy_redirect     default;
      proxy_http_version 1.1;
      proxy_set_header   Host $host;
      proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Proto $scheme;
      proxy_set_header   X-Forwarded-Port 443;
      proxy_max_temp_file_size 0;
      #this is the maximum upload size
      client_max_body_size       10m;
      client_body_buffer_size    128k;
      proxy_connect_timeout      90;
      proxy_send_timeout         90;
      proxy_read_timeout         90;
      proxy_set_header Connection ""; # Clear for keepalive
  }

}

这些设置中的大部分都来自故障排除指南,因为我最初的尝试没有将它们全部列出,我曾经并且仍然收到通知It appears that your reverse proxy set up is broken. 目前,它似乎只是部分工作。一些 url 可以正常工作,比如如果我点击人,我会得到https://abc.example.com:8003/asynchPeople/,但其他的比如登录和蓝海似乎放弃了端口。手动添加这个确实会使 url 然后工作。所以我不确定到底出了什么问题。我还应该补充一下,我已将 jenkins url 设置为 abc.example.com:8003

【问题讨论】:

    标签: docker nginx jenkins reverse-proxy


    【解决方案1】:

    经过大量阅读,以下内容对我的情况有所帮助。

    proxy_set_header   X-Forwarded-Host $http_host;
    

    这维护了端口号,并且功能似乎与 Jenkins 的预期一样。

    关于反向代理被破坏,我通过 curl 检查了管理任务。这失败了,给了我错误并将我重定向到这里:https://curl.haxx.se/docs/sslcerts.html。即使所有浏览器都显示安全图标并且没有问题。

    【讨论】:

    • 进一步补充卷曲问题。正是缺少中间证书导致了这个 curl 错误。一旦添加 curl 和其他系统很高兴
    • 在我的例子中,变量$http_host没有被成功替换,所以我不得不手动设置它的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2020-11-28
    • 2019-03-14
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多