如何在Ubuntu18.04上使用Nginx反向代理使用SSL配置Jenkins

来自菜鸟教程
跳转至:导航、​搜索

介绍

Jenkins默认自带内置Winstone web server监听端口8080,方便上手。 但是,使用 SSL 保护 Jenkins 以保护通过 Web 界面传输的密码和敏感数据也是一个好主意。

在本教程中,您将 Nginx 配置为反向代理,以将客户端请求定向到 Jenkins。

先决条件

首先,您需要以下内容:

第 1 步 — 配置 Nginx

在先决条件教程 How to Secure Nginx with Let's Encrypt on Ubuntu 18.04 中,您在 /etc/nginx/sites-available/example.com 文件中将 Nginx 配置为使用 SSL。 打开此文件以添加您的反向代理设置:

sudo nano /etc/nginx/sites-available/example.com

在带有 SSL 配置设置的 server 块中,添加特定于 Jenkins 的访问和错误日志:

/etc/nginx/sites-available/example.com

. . . 
server {
        . . .
        # SSL Configuration
        #
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        access_log            /var/log/nginx/jenkins.access.log;
        error_log             /var/log/nginx/jenkins.error.log;
        . . .
        }

接下来让我们配置代理设置。 由于我们将所有请求发送给 Jenkins,我们将注释掉默认的 try_files 行,否则会在请求到达 Jenkins 之前返回 404 错误:

/etc/nginx/sites-available/example.com

. . .
           location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;        }
. . . 

现在让我们添加代理设置,其中包括:

  • proxy_params/etc/nginx/proxy_params 文件由 Nginx 提供,确保重要信息,包括主机名、客户端请求的协议和客户端 IP 地址,在日志文件中保留并可用.
  • proxy_pass:设置代理服务器的协议和地址,在这种情况下,将是通过端口 8080 上的 localhost 访问的 Jenkins 服务器。
  • proxy_read_timeout:这使得从 Nginx 的 60 秒默认值增加到 Jenkins 推荐的 90 秒值。
  • proxy_redirect:这确保 响应被正确重写 以包含正确的主机名。

请务必在下面的 proxy_redirect 行中将您的 SSL 安全域名替换为 example.com

/etc/nginx/sites-available/example.com

Location /  
. . .
           location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                include /etc/nginx/proxy_params;
                proxy_pass          http://localhost:8080;
                proxy_read_timeout  90s;
                # Fix potential "It appears that your reverse proxy setup is broken" error.
                proxy_redirect      http://localhost:8080 https://example.com;

完成这些更改后,保存文件并退出编辑器。 在配置 Jenkins 之前,我们将推迟重启 Nginx,但我们现在可以测试我们的配置:

sudo nginx -t

如果一切正常,命令将返回:

Outputnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果没有,请修复任何报告的错误,直到测试通过。

注意: 如果您错误地配置了 proxy_pass(例如,通过添加斜杠),您将在 Jenkins Configuration 页面中得到类似于以下内容的内容。

如果您看到此错误,请仔细检查 Nginx 配置中的 proxy_passproxy_redirect 设置。


第 2 步 - 配置 Jenkins

为了让 Jenkins 与 Nginx 一起工作,您需要更新 Jenkins 配置,以便 Jenkins 服务器仅侦听 localhost 接口而不是所有接口 (0.0.0.0)。 如果 Jenkins 侦听所有接口,则可能可以在其原始未加密端口 (8080) 上访问它。

让我们修改 /etc/default/jenkins 配置文件来进行这些调整:

sudo nano /etc/default/jenkins

找到 JENKINS_ARGS 行并将 --httpListenAddress=127.0.0.1 添加到现有参数:

/etc/default/jenkins

. . .
JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT --httpListenAddress=127.0.0.1"

保存并退出文件。

要使用新的配置设置,请重启 Jenkins:

sudo systemctl restart jenkins

由于 systemctl 不显示输出,请检查状态:

sudo systemctl status jenkins

您应该在 Active 行中看到 active (exited) 状态:

Output● jenkins.service - LSB: Start Jenkins at boot time
   Loaded: loaded (/etc/init.d/jenkins; generated)
   Active: active (exited) since Mon 2018-07-09 20:26:25 UTC; 11s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 29766 ExecStop=/etc/init.d/jenkins stop (code=exited, status=0/SUCCESS)
  Process: 29812 ExecStart=/etc/init.d/jenkins start (code=exited, status=0/SUCCESS)

重启 Nginx:

sudo systemctl restart nginx

检查状态:

sudo systemctl status nginx
Output● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-07-09 20:27:23 UTC; 31s ago
     Docs: man:nginx(8)
  Process: 29951 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 29963 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 29952 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 29967 (nginx)

重新启动两台服务器后,您应该能够使用 HTTP 或 HTTPS 访问域。 HTTP 请求将自动重定向到 HTTPS,并且 Jenkins 站点将得到安全服务。

第 3 步 — 测试配置

现在您已启用加密,您可以通过重置管理密码来测试配置。 让我们首先通过 HTTP 访问该站点,以验证您是否可以访问 Jenkins 并被重定向到 HTTPS。

在您的网络浏览器中,输入 http://example.com,将 example.com 替换为您的域。 按 ENTER 后,URL 应以 https 开头,并且位置栏应指示连接是安全的。

您可以在 User 字段中输入您在 How To Install Jenkins on Ubuntu 18.04 中创建的管理用户名,并在 Password 字段中输入您选择的密码。

登录后,您可以更改密码以确保其安全。

单击屏幕右上角的用户名。 在主配置文件页面上,从页面左侧的列表中选择 Configure

这将带您进入一个新页面,您可以在其中输入并确认新密码:

单击保存确认新密码。 您现在可以安全地使用 Jenkins Web 界面。

结论

在本教程中,您将 Nginx 配置为 Jenkins 内置 Web 服务器的反向代理,以保护您的凭据和通过 Web 界面传输的其他信息。 现在 Jenkins 是安全的,您可以学习 如何设置持续集成管道 以自动测试代码更改。 如果您是 Jenkins 新手,需要考虑的其他资源是 Jenkins 项目的“创建您的第一个管道” 教程或 社区贡献插件库