使用 nginx http auth 保护 Jenkins(回调 url 除外)

2024-06-26

我在我的服务器上安装了 jenkins,我想用 nginx http auth 来保护它,以便请求:

http://my_domain.com:8080
http://ci.my_domain.com

除一个位置外将受到保护:

http://ci.my_domain.com/job/my_job/build

需要触发构建。我对 nginx 有点陌生,所以我坚持使用 nginx 配置。

upstream jenkins {
  server  127.0.0.1:8080;
}

server {
  listen x.x.x.x:8080;
  server_name *.*;

  location '/' {
    proxy_pass http://jenkins;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    auth_basic "Restricted";
    auth_basic_user_file /path/.htpasswd;    
  }
}

我尝试了上面的配置,但是当我访问时http://my_domain.com:8080没有 http 身份验证。


最后我想出了如何解决这个问题。首先,我们需要在管理 Jenkins 页面取消选中“启用安全性”选项。禁用安全性后,我们可以通过以下请求触发我们的工作http://ci.your_domain.com/job/job_name/build.

如果要添加令牌来触发 URL,我们需要启用安全性,选择“基于项目的矩阵授权策略”并为匿名用户授予管理员权限。在项目的“配置”页面中之后,将出现“触发远程构建”选项,您可以在其中指定令牌,以便您的请求如下所示JENKINS_URL/job/onru/build?token=TOKEN_NAME

因此,对于禁用的安全性,我们需要保护http://ci.your_domain.com http://ci.your_domain.com使用 nginx http_auth 除了 url 之外/job/job_name/build'.

当然,我们需要对外部请求隐藏 8080 端口。由于我的服务器在 Ubuntu 上,我可以使用iptables防火墙:

iptables -A INPUT -p tcp --dport 8080 -s localhost -j ACCEPT
iptables -A INPUT -p tcp --dport 8080 -j DROP

但!在 ubuntu 上(我不确定其他 Linux 操作系统)iptables 将在重新启动后消失。所以我们需要用以下方法保存它们:

iptables-save

但这还不是结束。使用这个命令我们只得到一个带有 iptables 的文件。启动时我们需要加载 iptables,最简单的方法是使用“uptables-persistent”包:

sudo apt-get install iptables-persistent
iptables-save > /etc/iptables/rules

如果需要的话,仔细看看 iptableshttps://help.ubuntu.com/community/IptablesHowTo#Saving_iptables https://help.ubuntu.com/community/IptablesHowTo#Saving_iptables祝詹金斯好运!

有一个在服务器的子域上运行 jenkins 的好例子:https://wiki.jenkins-ci.org/display/JENKINS/Running+Hudson+behind+Nginx https://wiki.jenkins-ci.org/display/JENKINS/Running+Hudson+behind+Nginx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 nginx http auth 保护 Jenkins(回调 url 除外) 的相关文章

随机推荐