How to whitelist a user agent for nginx?

Posted by djb on Server Fault See other posts from Server Fault or by djb
Published on 2012-11-29T16:53:01Z Indexed on 2012/12/02 23:11 UTC
Read the original article Hit count: 241

Filed under:

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.

© Server Fault or respective owner

Related posts about nginx