Nginx authentication with auth_request module

痞子三分冷 提交于 2019-11-29 02:24:53
PedroSena

The trick here is to combine auth_basic and auth_request, here is an example:

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;
    }

You will notice that auth_basic_user_file is present and you probably don't want it but you can leave a blank file, the satisfy any will accept any success, auth_basic will fail but will also set the user and password in the HTTP Headers that are forwarded to your backend script where you can handle them accordingly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!