# Restricting access to specific subdirectories

**URL:** <https://www.ruby-forum.com/t/restricting-access-to-specific-subdirectories/229668>\
**Category:** NGINX\
**Created:** [February 3, 2013, 4:28am UTC](https://www.ruby-forum.com/t/restricting-access-to-specific-subdirectories/229668 "2013-02-03T04:28:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdiana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@jdiana](https://www.ruby-forum.com/u/jdiana)\
**Post date:** [February 3, 2013, 4:28am UTC](https://www.ruby-forum.com/t/restricting-access-to-specific-subdirectories/229668/1 "2013-02-03T04:28:00Z")

</div>

Hey all,

I’m a little stumped about what I’m doing wrong here. Basically I have  
a  
subdirectory that I want to restrict access to specific IP’s, otherwise  
return a 403.

If I do the following (inside my server {} block):

server {  
// normal processing code here  
…

```
    location ~ ^/my_ws$ {
            allow XX.XX.XX.XX;
            allow XX.XX.XX.XX/24;
            deny all;
    }

```

}

Hitting the following URL works as intended and I get a 403 if I try  
from  
anywhere other than the specified URL’s: [http://www.mydomain.com/my\_ws](http://www.mydomain.com/my_ws)

However, if there’s anything AFTER that (i.e. my\_ws/, my\_ws/page2,  
my\_ws?parameter1, etc.) it allows them to proceed regardless of IP.

I’m sure it’s something required before or after the $, but I can’t  
figure  
it out.

Thanks in advance!

Justin

Posted at Nginx Forum:

> **[Restricting access to specific subdirectories](https://forum.nginx.org/read.php?2,235864,235864#msg-235864)**
>
> Hey all, I'm a little stumped about what I'm doing wrong here. Basically I have a subdirectory that I want to restrict access to specific IP's, otherwise return a 403. If I do the following (inside my server {} block): server { // normal p

---

<div class="post-metadata">

**Author:** ![jdiana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@jdiana](https://www.ruby-forum.com/u/jdiana)\
**Post date:** [February 3, 2013, 4:47am UTC](https://www.ruby-forum.com/t/restricting-access-to-specific-subdirectories/229668/2 "2013-02-03T04:47:17Z")

</div>

On 03/02/13 16:27, jdiana wrote:

> ```
> ...
> 
> ```

do you need a $ at all? It’s a placeholder for the end of the string,  
and all you care about s the start??

Steve

---

<div class="post-metadata">

**Author:** ![jdiana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@jdiana](https://www.ruby-forum.com/u/jdiana)\
**Post date:** [February 3, 2013, 12:53pm UTC](https://www.ruby-forum.com/t/restricting-access-to-specific-subdirectories/229668/3 "2013-02-03T12:53:58Z")

</div>

On 3 February 2013 03:27, jdiana [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[snip]

> ```
> location ~ ^/my_ws$ {
> 
> ```

[snip]

> However, if there’s anything AFTER that (i.e. my\_ws/, my\_ws/page2,  
> my\_ws?parameter1, etc.) it allows them to proceed regardless of IP.
> 
> I’m sure it’s something required before or after the $, but I can’t figure  
> it out.

Your problem is absolutely to do with the “$”, and if you don’t yet  
understand regex well enough to fix it, have a read through a simple  
intro such as [http://www.zytrax.com/tech/web/regex.htm#positioning](http://www.zytrax.com/tech/web/regex.htm#positioning).

BTW that’s a pointer to the exact section you need, but I recommend  
you digest at least the first half of that guide - it’s not long or  
difficult, and will serve you well in the future.

## HTH, Jonathan

Jonathan M. // Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![jdiana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@jdiana](https://www.ruby-forum.com/u/jdiana)\
**Post date:** [February 3, 2013, 10:53pm UTC](https://www.ruby-forum.com/t/restricting-access-to-specific-subdirectories/229668/4 "2013-02-03T22:53:25Z")

</div>

Hello!

On Sat, Feb 02, 2013 at 10:27:40PM -0500, jdiana wrote:

> ```
> ...
> 
> ```
> 
> However, if there’s anything AFTER that (i.e. my\_ws/, my\_ws/page2,  
> my\_ws?parameter1, etc.) it allows them to proceed regardless of IP.
> 
> I’m sure it’s something required before or after the $, but I can’t figure  
> it out.

You don’t need regular expressions, just use normal prefix  
location:

```
location /my_ws {
    allow ...
    deny all;
}

```

See [Module ngx\_http\_core\_module](http://nginx.org/r/location) for details.

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
