How to whitelist a user agent for nginx?

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.