# Can auth\_basic restrict files in certain folder?

**URL:** <https://www.ruby-forum.com/t/can-auth-basic-restrict-files-in-certain-folder/185908>\
**Category:** NGINX\
**Created:** [March 29, 2010, 4:33am UTC](https://www.ruby-forum.com/t/can-auth-basic-restrict-files-in-certain-folder/185908 "2010-03-29T04:33:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [March 29, 2010, 4:33am UTC](https://www.ruby-forum.com/t/can-auth-basic-restrict-files-in-certain-folder/185908/1 "2010-03-29T04:33:20Z")

</div>

Hi, all

I have some problem with my nginx  
I just could not restrict files in certain floder, as follows, i put the  
settings to nginx.conf

```
location ~ ^/restrict/
{
  auth_basic "STATISTIC";
  auth_basic_user_file /usr/local/nginx/conf/pwfornginx
}

```

the folder /restrict/ is restricted perfect, however, files in the  
folder can still be accessed,  
/restrict/somefile.php

What’s the matter? any ideas?

Thanks in advance.

Regards,

gavin

Posted at Nginx Forum:

> **[Can auth\_basic restrict files in certain folder?](https://forum.nginx.org/read.php?2,68890,68890#msg-68890)**
>
> Hi, all I have some problem with my nginx I just could not restrict files in certain floder, as follows, i put the settings to nginx.conf \[code\] location ~ ^/restrict/ { auth\_basic "STATISTIC"; auth\_ba

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [March 29, 2010, 5:14am UTC](https://www.ruby-forum.com/t/can-auth-basic-restrict-files-in-certain-folder/185908/2 "2010-03-29T05:14:19Z")

</div>

Hi Gavin,

> the folder /restrict/ is restricted perfect, however, files in the folder can still be accessed,  
> /restrict/somefile.php
> 
> What’s the matter? any ideas?

The problem is you somewhat have to rethink how NginX process the  
request. read here for more details

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration)**
>
> nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port \*:80:

but to sum it up you probably have a something like  
location ~ .\*.php${  
}  
in your config which actually handles the /restrict/somefile.php instead  
of  
location ~ ^/restrict/ { }  
there is two ways to remidy this. you can do something like

location ~ ^/restrict/.\*.php$ {  
#rest of auth config  
}

or you can do sub-locations _note_ this is the only instance that i have  
read where it is safe to do nested locations. YMMV

location ~ ^/restrict/  
{  
auth\_basic “STATISTIC”;  
auth\_basic\_user\_file /usr/local/nginx/conf/pwfornginx  
location ~ .\*.php$ {  
#include your php config for processing ie proxy\_pass or  
fastcgi\_pass  
}  
}

v/r,  
Rob

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [March 29, 2010, 9:09am UTC](https://www.ruby-forum.com/t/can-auth-basic-restrict-files-in-certain-folder/185908/3 "2010-03-29T09:09:00Z")

</div>

## Rob S. Wrote:

> > ```
> > auth_basic_user_file  
> > 
> > ```
> 
> location ~ ^/restrict/ { }
> 
> v/r,  
> Rob
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Hi, Rob

Great thanks for your quick reply. I have tried your suggestion out, and  
add the following config:

```
location ~ ^/restrict/
{
  auth_basic "STATISTIC";
  auth_basic_user_file /usr/local/nginx/conf/pwfornginx
}
location ~ ^/restrict/.*\.php$
{
  auth_basic "STATISTIC";
  auth_basic_user_file /usr/local/nginx/conf/pwfornginx
}

```

However, it is still not working. No authorization is required when I  
try to get access to a certain page in the restricted folder.

V/R,

gavin

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,68890,68944#msg-68944](http://forum.nginx.org/read.php?2,68890,68944#msg-68944)

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [March 29, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/can-auth-basic-restrict-files-in-certain-folder/185908/4 "2010-03-29T15:18:21Z")

</div>

BTW, I should read tips in  
[http://nginx.org/en/docs/http/request\_processing.html#simple\_php\_site\_configuration](http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration)  
first.

Thanks again. 🙂

Posted at Nginx Forum:

> **[Re: Can auth\_basic restrict files in certain folder?](https://forum.nginx.org/read.php?2,68890,68929#msg-68929)**
>
> Hi, all I have some problem with my nginx I just could not restrict files in certain floder, as follows, i put the settings to nginx.conf \[code\] location ~ ^/restrict/ { auth\_basic "STATISTIC"; auth\_ba

---

<div class="post-metadata">

**Author:** ![WisdomFusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/898d66/32.png) [@WisdomFusion](https://www.ruby-forum.com/u/WisdomFusion)\
**Post date:** [March 29, 2010, 7:41am UTC](https://www.ruby-forum.com/t/can-auth-basic-restrict-files-in-certain-folder/185908/5 "2010-03-29T07:41:48Z")

</div>

Hi, Rob

Great thanks for your quick reply. I have tried your suggestion out, and  
add the following config:  
location ~ ^/restrict/  
{  
auth\_basic “STATISTIC”;  
auth\_basic\_user\_file /usr/local/nginx/conf/pwfornginx  
}  
location ~ ^/restrict/.\*.php$  
{  
auth\_basic “STATISTIC”;  
auth\_basic\_user\_file /usr/local/nginx/conf/pwfornginx  
}

However, it is still not working. No authorization is required when I  
try to get access to a certain page in the restricted folder.

V/R,

gavin

Posted at Nginx Forum:

> **[Re: Can auth\_basic restrict files in certain folder?](https://forum.nginx.org/read.php?2,68890,68928#msg-68928)**
>
> Hi, all I have some problem with my nginx I just could not restrict files in certain floder, as follows, i put the settings to nginx.conf \[code\] location ~ ^/restrict/ { auth\_basic "STATISTIC"; auth\_ba
