跳转至

ntlm: Nginx ntlm 模块由 lua 实现

安装

如果您尚未设置 RPM 仓库订阅,请 注册。然后您可以继续以下步骤。

CentOS/RHEL 7 或 Amazon Linux 2

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 lua-resty-ntlm

CentOS/RHEL 8+、Fedora Linux、Amazon Linux 2023

dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install lua5.1-resty-ntlm

要在 NGINX 中使用此 Lua 库,请确保已安装 nginx-module-lua

本文档描述了 lua-resty-ntlm v0.2,于 2018 年 2 月 07 日发布。


Windows authentication 通常在公司内部使用。IIS 可以轻松启用 Windows authentication。对于 Nginx 用户,一些解决方案并不友好:Nginx Pro 提供 ntlm 模块,但并不免费;reverse proxy 必须首先设置其他服务器。

该项目受到 express-ntlmPyAuthenNTLM2 的启发。IIS 会为每个连接触发 Windows 认证场景。与 IIS 不同,该项目仅在第一次请求时触发 ntlm。认证完成后,http 头 Authorization:Bearer 将发送到浏览器,浏览器应将其放入每个请求包中以避免再次进行 ntlm。同时,http 头 X-Ntlm-UsernameX-Ntlm-Domain 将发送到上游。

注意: 在 ntlm 认证期间不要 set-cookie(#1175)

使用方法

  • 安装 OpenResty,它集成了 Nginx 和 LuaJIT
  • 安装 LuaRocks,因为 ntlm.lua 依赖于 structiconv 模块
  • 安装 struct 模块:sudo /usr/local/openresty/luajit/bin/luarocks install struct
  • 安装 iconv 模块:sudo /usr/local/openresty/luajit/bin/luarocks install lua-iconv
  • ntlm.lua 保存到 /usr/local/openresty/site/lualib
  • 将以下代码添加到 /usr/local/openresty/nginx/conf/nginx.conf
        lua_shared_dict ntlm_cache 10m;
        keepalive_timeout  35;
        ... ...
        access_by_lua_block {
            local cache = ngx.shared.ntlm_cache
            require('ntlm').negotiate("ldap://domain.net:389", cache, 10)
            -- cache 是共享的 DICT
            -- timeout 小于 keepalive
        }
    
  • 重启 nginx 服务:sudo service openresty restart

GitHub

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