一次项目中使用开源nginx反向代理NTLM的windows身份验证出现反复登陆框,最终分析属于keepalive 在NTLM认证过程中发生变化导致。

如采用nginx plus版本,可以直接在在upstream区域添加专用的语句 ntlm;

upstream http_backend {
    server 127.0.0.1:8080;

    ntlm; # 允许使用NTLM身份验证代理请求 。一旦客户端发送带有以“ Negotiate”或“ NTLM”开头的“ Authorization”头字段值的请求,则上游连接将绑定到客户端连接。其他客户端请求将通过相同的上游连接进行代理,同时保留身份验证上下文。
    # Allows proxying requests with NTLM Authentication. The upstream connection is bound to the client connection once the client sends a request with the “Authorization” header field value starting with “Negotiate” or “NTLM”. Further client requests will be proxied through the same upstream connection, keeping the authentication context.

}

server {
    ...

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        ...
    }
}

如果不是nginx plus版本,就可以直接放弃了.

根据网上其他教程配置,可以实现登录功能,但存在bug. 因为上游链接和客户端链接没有绑定(参考上面ntlm参数说明),可能出现A用户获取B用户登录信息的问题(多用户同时访问系统时).

upstream http_backend {
    server 127.0.0.1:8080;
    keepalive_requests 2; # 添加这一语句尝试解决上面提到的问题.设置通过一个keepalive连接可以处理的最大请求数。达到最大请求数后,连接将关闭。
    # 将最大请求数设置为2,刚好可以满足一次NTLM身份认证,多用户同时访问时不能成功登录,但是可以防止上面提到的问题.没有进行详细测试.

    keepalive 16;
}

server {
    ...

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        ...
    }
}

(完)