我已经安装了启用了 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_basic
和auth_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/