nginx 反代理 Jenkins

配置

 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 userContent folder
            # note : This is the $JENKINS_HOME dir
            root /var/jenkins_home/;
            if (!-f $request_filename){
            # this file does not exist, might be a directory or a /**view** url
            rewrite (.*) /$1 last;
            break;
            }
            sendfile on;
        }
        #配置静态资源 解决js css文件无法加载无法访问的问题,注意末尾不能有 /
        location ~ "^/jenkins.*\.(js|css|jpg|jpeg|gif|png|ico|pdf|txt)$" {
            proxy_pass http://upstream_jenkins;
        }
        location /jenkins {

            sendfile off;
            proxy_pass         http://upstream_jenkins;
            proxy_redirect     default;
            proxy_http_version 1.1;

            # Required for Jenkins websocket agents
            proxy_set_header   Connection        $connection_upgrade;
            proxy_set_header   Upgrade           $http_upgrade;

            proxy_set_header   Host              $host;
            proxy_set_header   X-Real-IP         $remote_addr;
            proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   X-Forwarded-Host  $http_host;
            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_buffering            off;
            proxy_request_buffering    off; # Required for HTTP CLI commands
            proxy_set_header Connection ""; # Clear for keepalive
        }

jekins 静态文件

注意点

在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

链接

  1. nginx 反向代理 某个目录下 带特定后缀名的文件open in new window
  2. 关于nginx反代jenkins报错 反向代理设置有误open in new window
Last Updated:
Contributors: haizhiguang