我有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.,目前看来,它仅能部分起作用。有些网址可以正常工作,例如,如果我单击别人,我将得到https://abc.example.com:8003/asynchPeople/,但其他网址(例如登录名和蓝色海洋)似乎删除了该端口。手动将其重新添加确实可以使网址正常工作。所以我不确定到底是什么问题。我还应该添加我将 Jenkins 网址设置为abc.example.com:8003

最佳答案

经过大量阅读后,以下内容帮助我解决了问题。

proxy_set_header   X-Forwarded-Host $http_host;

这样可以保持端口号,并且功能似乎与Jenkins一样。

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

关于docker - Nginx反向代理背后的Jenkins并非在所有情况下都重定向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61127262/

10-16 17:28