# Is there a method to allow a particular user agent access to a server rule that uses the access and

**URL:** <https://www.ruby-forum.com/t/is-there-a-method-to-allow-a-particular-user-agent-access-to-a-server-rule-that-uses-the-access-and/211377>\
**Category:** NGINX\
**Created:** [September 2, 2011, 9:27am UTC](https://www.ruby-forum.com/t/is-there-a-method-to-allow-a-particular-user-agent-access-to-a-server-rule-that-uses-the-access-and/211377 "2011-09-02T09:27:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [September 2, 2011, 9:27am UTC](https://www.ruby-forum.com/t/is-there-a-method-to-allow-a-particular-user-agent-access-to-a-server-rule-that-uses-the-access-and/211377/1 "2011-09-02T09:27:51Z")

</div>

Hello,

I have a configuration in a server rule that typically only allows  
access by either an auth basic request or by certain ip addresses.

Something along the lines of this:

```
 server {
     listen 80;

     charset off;
     server_name authsite;

     satisfy any;

     auth_basic "Auth Message";
     auth_basic_user_file xyz.passwd;

     # Allow Internal Network
     allow 192.168.1.0/24;
     deny all;

     # many includes and location directives below
 }

```

We have a use case were we need to allow an external agent to have  
access to this site.  
I’d rather not play whack-a-mole and keep adding ip addresses for this  
agent.  
At the same time I cannot give the agent an login and password, because  
we can’t control the URLs.

So I was wondering if there is a way to also allow access to this site  
based on the user agent?

I tried using an if directive but that is not working.

Thanks for any ideas

–

Zev B.  
Cerego Japan Inc  
Developer  
Development / Technology

ゼブ ブルット  
セレゴ・ジャパン株式会社  
技術開発  
開発者

Office : +81-3-3463-7266  
Email : removed\_email\_address@domain.invalid

Corporate website: [http://www.cerego.com](http://www.cerego.com)  
Core service website: [http://iknow.jp](http://iknow.jp)

---

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [September 2, 2011, 10:31am UTC](https://www.ruby-forum.com/t/is-there-a-method-to-allow-a-particular-user-agent-access-to-a-server-rule-that-uses-the-access-and/211377/2 "2011-09-02T10:31:29Z")

</div>

Hello!

On Fri, Sep 02, 2011 at 04:27:08PM +0900, Zev B. wrote:

> ```
> charset off;
> 
> ```
> 
> So I was wondering if there is a way to also allow access to this  
> site based on the user agent?
> 
> I tried using an if directive but that is not working.

Something like this should work:

```
server {
    ...

    location / {
        error_page 418 = @allowed;

        if ($http_user_agent ~ something) {
            return 418;
        }

        satisfy any;

        allow ...
        auth_basic ...
    }

    location @allowed {
        # ...
    }
}

```

Alternatively, you may use auth request module[1] and write something  
like this:

```
server {
    ...

    location / {
        satisfy any;

        allow ...
        auth_basic ...
        auth_request /auth;
    }

    location = /auth {
        if ($http_user_agent ~ something) {
            return 200;
        }
        return 403;
    }
}

```

[1] [ngx\_http\_auth\_request\_module: log](http://mdounin.ru/hg/ngx_http_auth_request_module/)

Maxim D.

---

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [September 2, 2011, 11:35am UTC](https://www.ruby-forum.com/t/is-there-a-method-to-allow-a-particular-user-agent-access-to-a-server-rule-that-uses-the-access-and/211377/3 "2011-09-02T11:35:09Z")

</div>

Hello,

On 09/02/2011 05:31 PM, Maxim D. wrote:

> On Fri, Sep 02, 2011 at 04:27:08PM +0900, Zev B. wrote:

> > I tried using an if directive but that is not working.  
> > allow …  
> > }
> 
> [1] [ngx\_http\_auth\_request\_module: log](http://mdounin.ru/hg/ngx_http_auth_request_module/)

Thanks for the link to the auth request module  
That might serve my purposes best without have to refactor and duplicate  
my configs as much as the extra location will.

Thanks,  
Zev
