# Nginx is too greedy with urls

**URL:** <https://www.ruby-forum.com/t/nginx-is-too-greedy-with-urls/244543>\
**Category:** NGINX\
**Created:** [April 29, 2016, 4:11pm UTC](https://www.ruby-forum.com/t/nginx-is-too-greedy-with-urls/244543 "2016-04-29T16:11:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [April 29, 2016, 4:11pm UTC](https://www.ruby-forum.com/t/nginx-is-too-greedy-with-urls/244543/1 "2016-04-29T16:11:29Z")

</div>

Hi,

I have a case where a redirect for

word-hyphen

should take precendence over a proxy\_pass with

word

only

both are inside location {} directives.

location ~ ^/word-hyphen{  
return 301 [https://bla/one/two/word-hyphen](https://bla/one/two/word-hyphen) ;  
}

location /word{  
proxy\_pass [http://upstream/](http://upstream/) ;  
}

I just can’t get it to work.

Any ideas?

Best Regards,  
Rainer

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 29, 2016, 8:03pm UTC](https://www.ruby-forum.com/t/nginx-is-too-greedy-with-urls/244543/2 "2016-04-29T20:03:59Z")

</div>

If you’re from the apache world, nginx configuration if often  
counter-intuitive.

Does the following help?

# To find location matching a given request, nginx first checks

locations  
defined

# using the prefix strings (prefix locations). Among them, the location

with  
the

# longest matching prefix is selected and remembered.

# Then regular expressions are checked, in the order of their appearance

in

# the configuration file.

# The first matching expression stops the search and nginx will use this

location.

# If the longest matching prefix location has the ‘^~’ modifier then

regular

# expressions are not checked.

# Also, using the ‘=’ modifier it is possible to define an exact match

of  
URI

# and location. If an exact match is found, the search terminates.

Posted at Nginx Forum:  
[https://forum.nginx.org/read.php?2,266524,266527#msg-266527](https://forum.nginx.org/read.php?2,266524,266527#msg-266527)
