Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

php - Nginx authentication with auth_request module

i have installed nginx with auth_request module enabled, but i have a problem when i am trying to setup the authentication. I want to authenticate through a php script, when a user makes request to this location, then the nginx request to a php file and if the response will be 2xx then authentication true if the response will be 4xx then authentication failed.

This is what i made for now and it is working perfect this thing but i dont know how to pass arguments on the php file like username password for example: http://example.com/live/index.php?username=test&password=password

Here is the configuration which is working without these arguments.

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

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...