7

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

我启用的基本 http auth 有效:

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;
}

如果我有user1 , user2 infile_auser2 , user3 in file_b,这可行,但是当我更改user2的密码时,我必须更新这两个文件(所有位置的密码都应该相同)。由于我将拥有超过 15 个具有不同访问权限的不同位置和超过 10 个用户,这并不容易处理。(我喜欢细粒度的访问权限!)

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

有没有类似的东西,或者如何使用 nginx 轻松处理这种情况?

4

3 回答 3

15

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

配置中的小改动(参见第二个位置的 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
于 2012-07-19T21:22:58.503 回答
2

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

  • 对于每个组,我都有一个单独的密码文件,例如group_a.auth, group_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
  • 更新用户密码:update password inpasswords.txt并执行update.rb
  • 将用户添加到组(例如添加new_usergroup_a):打开group_a.auth并添加行new_user:。然后添加new_user:password_hashpasswords.txt如果用户不存在并最终运行update.rb
于 2014-04-08T16:42:39.053 回答
1

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

该脚本的当前版本是:

#! /usr/bin/env perl

use strict;

die "Usage: $0 USERSFILE GROUPSFILE\n" 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 /[ \t\r\n]+/, $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/

于 2016-09-16T14:52:54.887 回答