php - 使用 auth_request 模块的 Nginx 身份验证

标签 php authentication nginx auth-request

我已经安装了启用了 auth_request 模块的 nginx,但是当我尝试设置身份验证时遇到了问题。我想通过 php 脚本进行身份验证,当用户向此位置发出请求时,nginx 请求到 php 文件,如果响应为 2xx,则身份验证为真,如果响应为 4xx,则身份验证失败。

这是我现在所做的,它正在完美地工作,但我不知道如何在 php 文件上传递参数,例如用户名密码: http://example.com/live/index.php?username=test&password=password

这是在没有这些参数的情况下工作的配置。

location /live {
         auth_request /http_auth;
    }

    location /http_auth {
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header X-Original-URI $request_uri;
        proxy_pass http://127.0.0.1/login.php;
}

谢谢

最佳答案

这里的技巧是结合auth_basicauth_request,这里是一个例子:

location = /api {
        satisfy any;
        auth_basic "Restricted Access";
        auth_basic_user_file "/usr/local/nginx/htpasswd";
        auth_request /auth;
        try_files $uri $uri/ /api.html;
    }

    location = /auth {
       proxy_pass http://localhost:8080;
       proxy_pass_request_body off;
       proxy_set_header Content-Length "";
       proxy_set_header X-Original-URI $request_uri;
    }

你会注意到 auth_basic_user_file 存在,你可能不想要它,但你可以留下一个空白文件,satisfy any 将接受任何成功,auth_basic 将失败,但还会在转发到后端脚本的 HTTP header 中设置用户和密码,您可以在其中进行相应处理。

关于php - 使用 auth_request 模块的 Nginx 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299623/

相关文章:

nginx - 在 nginx 配置的 add_header 中放置换行符可以吗?

docker - Nginx 缓存 DNS 查找并忽略我的解析器设置

php - 通过电子邮件将内容发送到 php 脚本

php - 如何在 php 中查找余数和商数?

javascript - SugarCRM 7 - 如何在 View 之间共享代码

php - 多语言站点: Cookie or URL Element

mongodb - 无法使用 --db 创建备份 mongodump。身份验证失败

laravel - Tymon\JWTAuth::toUser 错误:需要 token

authentication - 使用 AppCmd 设置特定的 IIS 7 站点?

node.js - 通过 SSL 使用 Socket.io Node.js 的错误网关错误