Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
nginx.2469901.n2.nabble.com/… –  quanta Dec 1 '12 at 9:38

2 Answers 2

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.

share|improve this answer
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.