【问题标题】:NGINX - Limit access to folder to a list of user from ldap authenticationNGINX - 将文件夹的访问权限限制为来自 ldap 身份验证的用户列表
【发布时间】:2021-10-12 07:41:29
【问题描述】:

A 在 ldap 身份验证后面有一个 nginx 反向代理。 我可以从变量 $_SERVER['PHP_AUTH_USER'] 中读取 php 中的用户名。我认为这意味着用户名从 ldap 传递到 nginx,而不是传递到 php。
在 nginx 配置中是否可以只允许用户列表访问文件夹?

更新
在 nginx 中,用户存储在 $remote_user 变量中。是否可以将 $remote_user 与存储在文件中的用户列表进行比较?然后拒绝或允许访问文件夹?

更新
可能我必须使用 ma​​p 指令,例如:

map $remote_user $allowed_user {
    default 0;
    user1   1;
    user2   1;
}

然后在合适的位置进行测试:

location /folder/ {
    if($allowed_user != 1){
        return 403;
    }
    proxy_pass http://site;
}

但是当我执行sudo nginx -t 时,我收到以下错误:

nginx: [emerg] unknown directive "if($allowed_user" in /etc/nginx/nginx.conf:104
nginx: configuration file /etc/nginx/nginx.conf test failed

【问题讨论】:

    标签: authentication nginx


    【解决方案1】:

    您可以通过map 指令来实现(请注意map 翻译定义块应该放在server 块之外的http 上下文中):

    map $remote_user $deny
        username1  0;
        username2  0;
        ...
        usernameN  0;
        default    1;
    }
    
    server {
        ...
        location /folder/ {
            if ($deny) { return 403; }
            ...
        }
    }
    

    您可以预先生成上述表格中的用户列表(username1 0; username 2 0; ...),然后将此列表包含到 nginx 配置中:

    map $remote_user $deny {
        include /path/userlist.txt;
        default 1;
    }
    

    每当此用户列表文件发生更改时,您都需要重新加载 nginx 配置 (nginx -s reload)。

    【讨论】:

    • 非常感谢!请阅读我的更新...
    • nginx 配置语法不允许您跳过 if 指令及其条件之间的空格。你自己看不到 nginx 将 if($allowed_user 作为整个未知指令名称吗?在if 关键字后面加一个空格就可以了。
    • 谢谢,我添加了空间,现在可以使用了!
    • 然后你可以accept 回答:) BTW 默认map 翻译值是一个空字符串,除非用default 关键字指定了其他内容,所以使用你的if 块你可以安全删除 default 0; 行。
    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2015-11-10
    相关资源
    最近更新 更多