使用nginx http auth保护Jenkins,但回调网址除外

mak*_*ni4 8 proxy nginx http-authentication jenkins

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

http://my_domain.com:8080
http://ci.my_domain.com
Run Code Online (Sandbox Code Playgroud)

除一个地点外,将受到保护:

http://ci.my_domain.com/job/my_job/build
Run Code Online (Sandbox Code Playgroud)

需要触发构建.我是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;    
  }
}
Run Code Online (Sandbox Code Playgroud)

我试过像上面配置的smth但是当我访问时http://my_domain.com:8080没有http auth.

mak*_*ni4 7

最后我想出了如何解决这个问题.首先,我们需要在Manage Jenkins页面取消选中"启用安全性"选项.随着安全关闭,我们可以触发我们的工作与像请求http://ci.your_domain.com/job/job_name/build.

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

因此,对于禁用安全性,我们需要使用nginx http_auth 来保护http://ci.your_domain.com,除了网址之外/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
Run Code Online (Sandbox Code Playgroud)

但!在ubuntu上(我不确定其他linux ose)iptables会在重启后消失.所以我们需要保存它们:

iptables-save
Run Code Online (Sandbox Code Playgroud)

它不是结束.使用此命令,我们只需使用iptables获取文件.在启动时我们需要加载iptables,最简单的方法是使用'uptables-persistent'包:

sudo apt-get install iptables-persistent
iptables-save > /etc/iptables/rules
Run Code Online (Sandbox Code Playgroud)

如果需要,请仔细查看iptables https://help.ubuntu.com/community/IptablesHowTo#Saving_iptables并祝詹金斯好运!

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