4

I'm trying to figure out how to whitelist a user agent from my nginx conf. All other agents should be shown a password prompt.

In my naivity, I tried to put the following in before deny all:

if ($http_user_agent ~* SpecialAgent ) { allow; }

but I'm told "allow" directive is not allowed here (!).

How can I make it work?

A chunk of my config file:

server {
    server_name site.com;
    root /var/www/site;

    auth_basic            "Restricted";
    auth_basic_user_file  /usr/local/nginx/conf/htpasswd;

    allow 123.456.789.123;
    deny all;

    satisfy any;

    #other stuff...
}

Thanks for any help.

1

2 Answers 2

3

If only need to whitelist by user agent (not IP or other conditions) and don't want to recompile Nginx just to include the auth_request module you can also use the map directive to check the user agent and conditionally disable basic auth.

map $http_user_agent $auth_type {
    default "Restricted";
    ~^Mozilla "off";
}

server {
    location / {
        auth_basic $auth_type;
    }
}

This will turn off basic auth for Mozilla user agents.

0

You can use the auth_request module to achieve this.

Example at http://nginx.2469901.n2.nabble.com/Is-there-a-method-to-allow-a-particular-user-agent-access-to-a-server-rule-that-uses-the-access-and--td6752995.html as per the comment by @quanta.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .