使用auth_request模块进行Nginx身份验证

use*_*046 6 php authentication nginx auth-request

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

这就是我现在所做的,它是完美的工作,但我不知道如何传递php文件的参数,如用户名密码,例如:http://example.com/live/index.php? username = test&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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Ped*_*ena 10

这里的关键是要结合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;
    }
Run Code Online (Sandbox Code Playgroud)

您会注意到auth_basic_user_file存在并且您可能不想要它但是您可以留下空白文件,satisfy any将接受任何成功,auth_basic将失败但是还将在HTTP标头中设置转发到您的后端脚本的用户和密码你可以在哪里处理它们.