跳转至

auth-ldap: NGINX 的 LDAP 认证模块

安装

您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:

  • RedHat Enterprise Linux 7、8、9 和 10
  • CentOS 7、8、9
  • AlmaLinux 8、9
  • Rocky Linux 8、9
  • Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-auth-ldap
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-auth-ldap

通过在 /etc/nginx/nginx.conf 顶部添加以下内容来启用该模块:

load_module modules/ngx_http_auth_ldap_module.so;

本文档描述了 nginx-module-auth-ldap v0.3,发布于 2020 年 5 月 28 日。


nginx 的 LDAP 模块,支持对多个 LDAP 服务器的认证。

示例配置

定义您的 LDAP 服务器列表及所需的用户/组要求:

    http {
      ldap_server test1 {
        url ldap://192.168.0.1:3268/DC=test,DC=local?sAMAccountName?sub?(objectClass=person);
        binddn "TEST\\LDAPUSER";
        binddn_passwd LDAPPASSWORD;
        group_attribute uniquemember;
        group_attribute_is_dn on;
        require valid_user;
      }

      ldap_server test2 {
        url ldap://192.168.0.2:3268/DC=test,DC=local?sAMAccountName?sub?(objectClass=person);
        binddn "TEST\\LDAPUSER";
        binddn_passwd LDAPPASSWORD;
        group_attribute uniquemember;
        group_attribute_is_dn on;
        require valid_user;
      }
    }

并按照正确的顺序将所需的服务器添加到您的 location/server 指令中:

    server {
        listen       8000;
        server_name  localhost;

        auth_ldap "Forbidden";
        auth_ldap_servers test1;
        auth_ldap_servers test2;

        location / {
            root   html;
            index  index.html index.htm;
        }

    }

可用的配置参数

url

期望值:字符串

可用的 URL 方案:ldap://, ldaps://

binddn

期望值:字符串

binddn_passwd

期望值:字符串

group_attribute

期望值:字符串

group_attribute_is_dn

期望值:on 或 off,默认值为 off

require

期望值:valid_user, user, group

satisfy

期望值:all, any

max_down_retries

期望值:数字,默认值为 0

在 LDAP 服务器被认为“DOWN”时,尝试重新连接的重试次数。这可能发生在与 LDAP 服务器的 KEEP-ALIVE 连接超时或在一段时间后被服务器端终止时。

这通常可以帮助解决以下错误:

http_auth_ldap: ldap_result() failed (-1: Can't contact LDAP server)

connections

期望值:大于 0 的数字

ssl_check_cert

期望值:on 或 off,默认值为 off

验证 LDAPs 连接的远程证书。如果禁用,任何远程证书都将被接受,这可能使您面临中间人攻击的风险。请注意,如果启用此选项,服务器的证书需要由您的系统信任的合适 CA 签名。请参见下面如何在不全局安装 CA 的情况下信任 CA。

此选项需要 OpenSSL >= 1.0.2;如果使用较旧版本编译,则不可用。

ssl_ca_file

期望值:文件路径

信任此文件中的 CA 证书(见上面的 ssl_check_cert)。

ssl_ca_dir

期望值:目录路径

信任此目录中的所有 CA 证书(见上面的 ssl_check_cert)。

请注意,您需要在该目录中提供基于哈希的符号链接才能使其工作;您基本上需要在该目录中运行 OpenSSL 的 c_rehash 命令。

referral

期望值:on, off

LDAP 库的默认值为 on。此选项禁用来自 LDAP 服务器的引用消息的使用。对于对只读 AD 服务器进行身份验证而没有读写访问权限时非常有用。

GitHub

您可以在 nginx-module-auth-ldap 的 GitHub 仓库 中找到此模块的其他配置提示和文档。