# Turn basic authentication on and off for specific HTTP user agent

**URL:** <https://www.ruby-forum.com/t/turn-basic-authentication-on-and-off-for-specific-http-user-agent/224782>\
**Category:** NGINX\
**Created:** [November 26, 2012, 9:25am UTC](https://www.ruby-forum.com/t/turn-basic-authentication-on-and-off-for-specific-http-user-agent/224782 "2012-11-26T09:25:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 26, 2012, 9:25am UTC](https://www.ruby-forum.com/t/turn-basic-authentication-on-and-off-for-specific-http-user-agent/224782/1 "2012-11-26T09:25:06Z")

</div>

Hello All!

Is it possible to turn authentication on and off for a specific user  
agent  
in some location? When I configure the following

location /specloc/ {  
if ($http\_user\_agent ~ MSIE) {  
auth\_basic “private area”;  
auth\_basic\_user\_file /etc/nginx/htpasswd;  
}  
#…  
}

my “nginx -t” prints

nginx: [emerg] “auth\_basic” directive is not allowed here in  
/etc/nginx/nginx.conf:75  
nginx: configuration file /etc/nginx/nginx.conf test failed

Thank you if you answer.

Posted at Nginx Forum:

> **[Turn basic authentication on and off for specific HTTP user agent](https://forum.nginx.org/read.php?2,233214,233214#msg-233214)**
>
> Hello All! Is it possible to turn authentication on and off for a specific user agent in some location? When I configure the following location /specloc/ { if ($http\_user\_agent ~ MSIE) { auth\_basic "private are

---

<div class="post-metadata">

**Author:** ![hide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a4c791/32.png) [@hide](https://www.ruby-forum.com/u/hide)\
**Post date:** [December 10, 2012, 2:32pm UTC](https://www.ruby-forum.com/t/turn-basic-authentication-on-and-off-for-specific-http-user-agent/224782/2 "2012-12-10T14:32:21Z")

</div>

On Mon, Nov 26, 2012 at 03:24:20AM -0500, hide wrote:

> #…  
> }
> 
> my “nginx -t” prints
> 
> nginx: [emerg] “auth\_basic” directive is not allowed here in  
> /etc/nginx/nginx.conf:75  
> nginx: configuration file /etc/nginx/nginx.conf test failed
> 
> Thank you if you answer.

[http://trac.nginx.org/nginx/changeset/4946/nginx](http://trac.nginx.org/nginx/changeset/4946/nginx)

As of this change, you can do it like this:

```
map $http_user_agent $realm {
    default off;
    ~MSIE "private area";
}

server {
    location /specloc/ {
        auth_basic $realm;
        auth_basic_user_file /etc/nginx/htpasswd;
    }
}

```
