# Location directive not working

**URL:** <https://www.ruby-forum.com/t/location-directive-not-working/240059>\
**Category:** NGINX\
**Created:** [March 26, 2014, 1:30am UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059 "2014-03-26T01:30:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [March 26, 2014, 1:30am UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/1 "2014-03-26T01:30:54Z")

</div>

I set up two upstream servers (server 1 and server 2) where I geotarget  
people from specific countries: A

People from country A go to server 1 and the others go to server 2.

There are some uri s that must be served by server 1 when the person is  
in  
country A. So I have created this directive. It works only when I  
include  
one item, when I do (videos|events) it does not work

location ^~ /(videos|events) {  
proxy\_pass http://$server1$request\_uri;  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;

}

I do it this way because there are URLs that contain videos and events  
that must be served by server 1. For example,  
[domain.com/videos/playing-with-my-dog](http://domain.com/videos/playing-with-my-dog) etc. When I reload this directive  
nginx does not give me any errors, but when I try to access it from  
country B, it does not work.

Thanks for all your help!

Alex

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2014, 10:03am UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/2 "2014-03-26T10:03:51Z")

</div>

On Wed, Mar 26, 2014 at 12:30:21AM -0000, Alex wrote:

Hi there,

> It works only when I include  
> one item, when I do (videos|events) it does not work
> 
> location ^~ /(videos|events) {

That is not a regex location. A request for /videos/ will not match it.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)**
>
> This directive appeared in version 1.11.8.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2014, 1:32pm UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/3 "2014-03-26T13:32:11Z")

</div>

Francis -

Thank you for your reply!

This OR regex : location ~\* .(gif|jpg|jpeg)$

works on jogs and gifs, why can not I use the same syntax for the URIs  
as  
I am doing it?

Does it mean then that I have to define each location block one by one  
with different URIs even though all the information inside the location  
block is the same? Is there a regex to match the OR condition just as  
you  
do with jogs?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2014, 2:35pm UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/4 "2014-03-26T14:35:23Z")

</div>

On Wed, Mar 26, 2014 at 12:31:30PM -0000, Alex wrote:

Hi there,

> This OR regex : location ~\* .(gif|jpg|jpeg)$

Which part of that line says that it is a regex?

(The answer is in the documentation.)

> works on jogs and gifs, why can not I use the same syntax for the URIs as  
> I am doing it?

If you used the same syntax, it would probably work.

> > > location ^~ /(videos|events) {

is not the same syntax.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2014, 5:45pm UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/5 "2014-03-26T17:45:19Z")

</div>

Francis -

What I meant was that I was using an OR statement as it is used in the  
jpg  
example. If I do this:

location ^~ /videos/ { # it works

location ^~ /(videos|events) { #it does not work. So I assume is the OR  
that does not work.

Any thoughts?

Thanks,

Alex

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2014, 6:38pm UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/6 "2014-03-26T18:38:53Z")

</div>

## Wow Francis. You are a patient man. I love the way you teach things. I mean it all.

_B. R._

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2014, 6:15pm UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/7 "2014-03-26T18:15:20Z")

</div>

On Wed, Mar 26, 2014 at 04:44:49PM -0000, Alex wrote:

Hi there,

> location ^~ /(videos|events) { #it does not work. So I assume is the OR  
> that does not work.
> 
> Any thoughts?

The documentation is at [Module ngx\_http\_core\_module](http://nginx.org/r/location).

It’s all useful, but the fourth sentence is particularly relevant.

> > > This OR regex : location ~\* .(gif|jpg|jpeg)$
> > 
> > Which part of that line says that it is a regex?

When you know the answer to that question, you’ll probably see that the  
various squiggles after the word “location” aren’t just random.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [March 26, 2014, 10:39pm UTC](https://www.ruby-forum.com/t/location-directive-not-working/240059/8 "2014-03-26T22:39:01Z")

</div>

Thank you for your answer! I read the paragraph. I changed the  
preceding  
modifier to ~\* and it works now.

I made a mistake addressing the OR condition and calling it regex sorry.

Thank you for your help!
