Forum: NGINX Turn basic authentication on and off for specific HTTP user agent

Posted by hide (Guest)
on 2012-11-26 09:25
(Received via mailing list)
Hello All!

Is it possible to turn authentication on and off for a specific user 
agent
in some location? When I configure the following

location /specloc/ {
            if ($http_user_agent ~ MSIE) {
                  auth_basic            "private area";
                  auth_basic_user_file  /etc/nginx/htpasswd;
            }
#...
}

my "nginx -t" prints

nginx: [emerg] "auth_basic" directive is not allowed here in
/etc/nginx/nginx.conf:75
nginx: configuration file /etc/nginx/nginx.conf test failed

Thank you if you answer.

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,233214,233214#msg-233214
Posted by Ruslan Ermilov (Guest)
on 2012-12-10 14:32
(Received via mailing list)
On Mon, Nov 26, 2012 at 03:24:20AM -0500, hide wrote:
> #...
> }
>
> my "nginx -t" prints
>
> nginx: [emerg] "auth_basic" directive is not allowed here in
> /etc/nginx/nginx.conf:75
> nginx: configuration file /etc/nginx/nginx.conf test failed
>
> Thank you if you answer.

http://trac.nginx.org/nginx/changeset/4946/nginx

As of this change, you can do it like this:

    map $http_user_agent $realm {
        default off;
        ~MSIE   "private area";
    }

    server {
        location /specloc/ {
            auth_basic $realm;
            auth_basic_user_file /etc/nginx/htpasswd;
        }
    }
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.