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


Nginx authentication with auth_request module

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

这就是我现在所做的,它非常完美,但我不知道如何在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;
}

感谢

这里的技巧是组合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标头中设置用户和密码,这些标头将转发到后端脚本,在那里您可以相应地处理它们。