Basic Auth only for external IPs and not localhost or LAN networks

Maxim Dounin mdounin at mdounin.ru
Wed Feb 15 09:09:32 UTC 2012


Hello!

On Wed, Feb 15, 2012 at 10:03:13AM +0530, Quintin Par wrote:

> Hi all,
> 
> I have a location directive with basic auth in it.
> 
>         location / {
> 
>                    auth_basic "Admin Login";
> 
>                    auth_basic_user_file /etc/nginx/.htpasswd;
> 
> How do I specify a rule such that the basic auth is applied only to
> external IPs and not to 127.0.0.x, 192.0.x & 10.0.x?

Use "satisfy any", see
http://www.nginx.org/en/docs/http/ngx_http_core_module.html#satisfy

    location / {
        satisfy any;

        auth_basic "Admin Login";
        auth_basic_user_file /etc/nginx/.htpasswd;

        allow 127.0.0.0/24;
        allow 192.0.0.0/16;
        allow 10.0.0.0/16;
        deny all;
    }

Just a side note: the "192.0.x" should probably be "192.168.x" 
instead, but you should get the idea anyway.

Maxim Dounin



More information about the nginx mailing list