nginx group http auth



来自 apache2 我唯一无法存档的功能:让用户在密码数据库 ( htpasswd ) 中并允许访问不同的文件/文件夹/虚拟服务器。

我启用的基本 http 身份验证工作:

location ~ ^/a/ {
    # should allow access for user1, user2
    auth_basic            "Restricted";
    auth_basic_user_file  /etc/nginx/auth/file_a;
}
location ~ ^/b/ {
    # should allow access for user2, user3
    auth_basic            "Restricted";
    auth_basic_user_file  /etc/nginx/auth/file_b;
}

如果我有用户 1用户 2file_a用户 2用户 3file_b,这有效,但是当我更改用户 2 的密码时,我必须更新这两个文件(密码应该对所有位置相同)。由于我将有>15 个具有不同访问权限的不同位置和>10 个用户,因此这并不容易处理。(我喜欢细粒度的访问权限!

使用 Apache,我为每个位置定义了不同的组,并需要正确的组。更改访问权限就像在组中添加/删除用户一样简单。

有没有这样的事情,或者如何使用nginx轻松处理这种情况?

您可以使用AuthDigest模块和领域作为组来使其工作 - 一个用户有多个条目,但您可以在单个文件中逐行将它们放在一个文件中。不完美,但比你现在的噩梦要好。

配置的小改动(请参阅第 2 个位置的 auth_digest 和user_file):

location ~ ^/a/ {
    # should allow access for user1, user2
    auth_digest            "Restricted";
    auth_digest_user_file  /etc/nginx/auth/file_a;
}
location ~ ^/b/ {
    # should allow access for user2, user3
    auth_digest            "Restricted2";
    auth_digest_user_file  /etc/nginx/auth/file_a;
}

和file_a:

user1:Restricted1:password_hash
user2:Restricted1:password_hash
user2:Restricted2:password_hash
user3:Restricted2:password_hash

我终于用基本的 http auth 这样管理它:

  • 对于每个组,我都有一个单独的密码文件,例如group_a.authgroup_b.auth,...
  • 此外,我有一个文件,其中写入了每个用户和密码,例如passwords.txt
  • passwords.txt具有与身份验证文件相同的格式,因此类似于user1:password_hash
  • 我有一个 ruby 脚本update.rb将用户的密码从password.txt同步到所有.auth文件(更多的是sed的包装器):

拼音脚本update.rb

#!/usr/bin/env ruby
passwords = File.new("./passwords.txt","r")
while pwline = passwords.gets
    pwline.strip!
    next if pwline.empty?
    user, _ = pwline.split(':')
    %x(sed -i 's/#{user}:.*/#{pwline.gsub('/','/')}/g' *.auth)
end
  • 更新用户密码:在passwords.txt中更新密码并执行update.rb
  • 要将用户添加到组(例如new_usergroup_a):打开group_a.auth并添加行new_user:。然后,如果用户尚不存在,则向passwords.txt添加new_user:password_hash,最后运行update.rb

我正在使用一个脚本 nginx-groups.pl 来解析 apache 风格的密码和组文件,并为每个组生成单独的密码文件。 因此,它本质上与 Markus 答案中的 Ruby 脚本执行相同的操作,但它对所有组仅使用一个文件,并且组文件的格式与 apache 相同。

该脚本的当前版本是:

#! /usr/bin/env perl
use strict;
die "Usage: $0 USERSFILE GROUPSFILEn" unless @ARGV == 2;
my ($users_file, $groups_file) = @ARGV;
my %users;
open my $fh, "<$users_file" or die "cannot open '$users_file': $!n";
while (my $line = <$fh>) {
    chomp $line;
    my ($name, $password) = split /:/, $line, 2;
    next if !defined $password;
    $users{$name} = $line;
}
open my $fh, "<$groups_file" or die "cannot open '$groups_file': $!n";
while (my $line = <$fh>) {
    my ($name, $members) = split /:/, $line, 2 or next;
    next if !defined $members;
    $name =~ s/[ t]//g;
    next if $name eq '';
    my @members = grep { length $_ && exists $users{$_} } 
                  split /[ trn]+/, $members;
    my $groups_users_file = $name . '.users';
    print "Writing users file '$groups_users_file'.n";
    open my $wh, ">$groups_users_file" 
        or die "Cannot open '$groups_users_file' for writing: $!n";
    foreach my $user (@members) {
        print $wh "$users{$user}n"
            or die "Cannot write to '$groups_users_file': $!n";
    }
    close $wh or die "Cannot close '$groups_users_file': $!n";
}

以您喜欢的任何名称保存它并使其可执行。 不带参数调用它将打印简短的使用信息。

有关详细信息,请参阅 http://www.guido-flohr.net/group-authentication-for-nginx/!

最新更新