如何为 Jenkins 设置 NGINX 代理?

How to setup NGINX proxy for Jenkins?

我有当前的 jenkins 配置:

server {
        listen 80;
        listen [::]:80;

        server_name server_name mysubdomain.maindomain.com;

    # This is the jenkins web root directory (mentioned in the /etc/default/jenkins file)
    root            /var/run/jenkins/war/;

        access_log      /var/log/nginx/jenkins/access.log;
        error_log       /var/log/nginx/jenkins/error.log;

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

    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}\/(.*)" / 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 (.*) / last;
            break;
            }
        sendfile on;
    }

        location @jenkins {
            sendfile off;
            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;

            # Required for new HTTP-based CLI
            proxy_http_version 1.1;
            proxy_request_buffering off;

            proxy_pass http://127.0.0.1:2021;
        }

         location / {
           # try_files $uri $uri/ =404;
               try_files $uri @jenkins;
         }
}

本质上是 this jenkins configuration 和我当前的 /etc/default/jenkins 文件的副本:

NAME=jenkins

# location of java
JAVA=/usr/bin/java
JAVA_ARGS="-Djava.awt.headless=true"

# make jenkins listen on IPv4 address
JAVA_ARGS="-Djava.net.preferIPv4Stack=true"

PIDFILE=/var/run/$NAME/$NAME.pid

JENKINS_USER=$NAME
JENKINS_GROUP=$NAME
JENKINS_WAR=/usr/share/$NAME/$NAME.war
JENKINS_HOME=/var/lib/$NAME

RUN_STANDALONE=true

JENKINS_LOG=/var/log/$NAME/$NAME.log

MAXOPENFILES=8192

HTTP_PORT=2021
HTTP_HOST=127.0.0.1

# servlet context, important if you want to use apache proxying
PREFIX=/$NAME


JENKINS_ARGS="--webroot=/var/cache/$NAME/war --prefix=$PREFIX --httpListenAddress=$HTTP_HOST --httpPort=$HTTP_PORT"

一个简单的 curl 请求显示了 Jenkins 的响应 运行:

$ curl http://localhost:2021/jenkins/
<html><head><meta http-equiv='refresh' content='1;url=/jenkins/login?from=%2Fjenkins%2F'/><script>window.location.replace('/jenkins/login?from=%2Fjenkins%2F');</script></head><body style='background-color:white; color:white;'>


Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:

Permission you need to have (but didn't): hudson.model.Hudson.Administer
-->

</body></html>

但是我无法从浏览器访问网络 UI。每当我尝试获得 404 时。以下是已安装“商品”的相关版本:

Nginx - 1.13.6
Jenkins - 2.73.2 (using java -jar path-to-warfile --version)
OS - ubuntu 16.04
JDK - openjdk version "1.8.0_131"

检查 sudo nginx -T 发现我的站点配置没有被加载。更正我的 nginx.conf 中的错误(目录的 include 指令中的拼写错误)后,问题就解决了。 感谢 IRC 上的 SmokedCheese his/her 帮助解决这个问题。