# How to catch a request that has a given word in url?

**URL:** <https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971>\
**Category:** NGINX\
**Created:** [August 9, 2010, 11:16am UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971 "2010-08-09T11:16:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 11:16am UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/1 "2010-08-09T11:16:53Z")

</div>

Hi,

I am trying to catch any request that has WORD in the url, eg:

catch: [http://example.org/whatever/path/WORD/file.pdf](http://example.org/whatever/path/WORD/file.pdf)

don’t catch: [http://example.org/some/other/path/anotherfile.jpg](http://example.org/some/other/path/anotherfile.jpg)

and if simple to do, don’t catch: [http://example.org/folder/WORD.doc](http://example.org/folder/WORD.doc)

I tried:

location ^~ /WORD/ {}

location WORD {}

location ~\* WORD {}

But none seemed to do want I want\*.

Thanks for your advice 🙂

\*: unless I’m hitting again this stupid caching thing, but this time I  
tried in safari and it didn’t work either, so it should be cleared.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 11:20am UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/2 "2010-08-09T11:20:50Z")

</div>

On Mon, Aug 09, 2010 at 11:16:53AM +0200, Fernando P. wrote:

> Thanks for your advice 🙂

Place

location ~ /WORD/ {  
…  
}

as the first location in a server.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 11:53am UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/3 "2010-08-09T11:53:30Z")

</div>

> Place
> 
> location ~ /WORD/ {  
> …  
> }
> 
> as the first location in a server.

I have placed as the first location of my server:

location ~ /WORD/ {  
internal;  
root …  
}

But it still gets served directly by nginx. I’d like to use it with  
X-Accel-Redirect, but first I need to make sure that people can’t access  
these files directly.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 11:59am UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/4 "2010-08-09T11:59:02Z")

</div>

I’ll use actual values:

A public request looks like:

/uploads/2009/product.jpg

An a restricted request looks like:

/uploads/2009/restricted/product.mp4

So I have:

location ~ /restricted/ {  
internal;  
root …;  
}

location ^~ /uploads/ {  
root …;  
}

Is it my /uploads/ location which fits maybe better the request and  
confuses nginx?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 11:57am UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/5 "2010-08-09T11:57:09Z")

</div>

On Mon, Aug 09, 2010 at 11:53:30AM +0200, Fernando P. wrote:

> location ~ /WORD/ {  
> internal;  
> root …  
> }
> 
> But it still gets served directly by nginx. I’d like to use it with  
> X-Accel-Redirect, but first I need to make sure that people can’t access  
> these files directly.

Could you create a debug log of the request:

> **[A debugging log](http://nginx.org/en/docs/debugging_log.html)**

?

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 12:20pm UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/6 "2010-08-09T12:20:13Z")

</div>

Finally I managed to make everything work.

> “^~” disables regex locaitons testing, so you need something liek this:  
> I never really understood the docs about that.

But I don’t really understand these ~, ^~ and what have you. They seem  
to affect other locations which is not obvious at first sight.

Thanks for your impressive support Igor.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/7 "2010-08-09T13:06:49Z")

</div>

On Mon, Aug 09, 2010 at 12:20:13PM +0200, Fernando P. wrote:

> Finally I managed to make everything work.
> 
> > “^~” disables regex locaitons testing, so you need something liek this:  
> > I never really understood the docs about that.
> 
> But I don’t really understand these ~, ^~ and what have you. They seem  
> to affect other locations which is not obvious at first sight.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 9, 2010, 12:04pm UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/8 "2010-08-09T12:04:45Z")

</div>

On Mon, Aug 09, 2010 at 11:59:02AM +0200, Fernando P. wrote:

> }
> 
> Is it my /uploads/ location which fits maybe better the request and  
> confuses nginx?

“^~” disables regex locaitons testing, so you need something liek this:

location ~ /restricted/ {  
internal;  
root …;  
}

location /uploads/ {  
root …;  
}

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 20, 2010, 5:45am UTC](https://www.ruby-forum.com/t/how-to-catch-a-request-that-has-a-given-word-in-url/192971/9 "2010-08-20T05:45:21Z")

</div>

> **[oxpdf.de](http://www.oxpdf.de/)**
>
> This domain may be for sale!

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,117748,121583#msg-121583)**
>
> A portal to and from the mailing list.
