根据sql数据库验证nginx请求
ws.nginxdbauth的Python项目详细描述
这个包通过查找 数据库中的用户/密码(mysql,postgresql,随便什么sqlalchemy) 支持)。
用法
配置数据库访问
您需要使用配置文件提供DSN和查询:
[default] dsn = postgresql://localhost/myusers query = SELECT password FROM users WHERE username = :username AND password = :password AND role = :x_required_role password_hash = bcrypt
有关支持的DSN,请参阅sqlalchemy documentation。注意你必须 安装相应的驱动程序python包(mysql-python,psycopg2, 等)你自己。
查询作为sql参数传递基本authusername和 password以及任何请求头(小写,和-替换 使用_)。(如果你的用途不够灵活,你就必须 暂时使用专门的查询运行单独的实例)。
查询必须返回用户的哈希密码。因为对于简单的情况 使用数据库提供的sql函数 散列密码(例如mysqlWHERE password=encrypt(:password, password)), 但是您也可以将passlib支持的任何散列指定为 password_hash在python中执行比较(这就是为什么我们需要 从数据库中存储的密码哈希)。你需要pip install passlib 使用此功能。
设置http服务
然后您需要设置一个http服务器,或者使用一个专用进程:
$ nginx-db-auth-serve --host localhost --port 8899 --config /path/to/config
或者作为一个cgi脚本,如果你有基础设施的话。 下面是一个apache配置片段示例:
ScriptAlias /nginx-auth /path/to/nginxdbauth/nginx-db-auth-cgi <Location /nginx-auth> SetEnv NGINXDBAUTH_CONFIG /path/to/config CGIPassAuth On # apache >= 2.4.13 # SetEnv NGINXDBAUTH_LOGFILE /path/to/log # optional, for debugging </Location>
配置nginx
现在您可以设置一个受保护的nginx位置,如下所示:
location /private/ { auth_request /auth; # ... define rest of location ... } location = /auth { proxy_pass http://localhost:8899; # or http://mycgi/nginx-auth proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header X-Required-Role "superuser"; proxy_set_header WWW-Authenticate "my realm"; }
nginx发送的WWW-Authenticate头将简单地在401上回显 请求(据我所知,nginx不提供在auth_requestcase中设置领域字符串的另一种方法)。
更改
1.0.2(2018-03-28)
- 回显401个响应的www authenticate头。
1.0.1(2018-03-28)
- 启用将错误记录到文件以调试CGI脚本。
1.0.0(2018-03-27)
- 第一次释放。