Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 {
    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
1… – quanta Dec 1 '12 at 9:38

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 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.