# Match any location except particular one

**URL:** <https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161>\
**Category:** NGINX\
**Created:** [November 23, 2011, 2:28pm UTC](https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161 "2011-11-23T14:28:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [November 23, 2011, 2:28pm UTC](https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161/1 "2011-11-23T14:28:09Z")

</div>

Hello, i have a website with the following files structure:

engine/  
\_\_index.php  
webroot/  
\_\_images/  
\_\_css/  
\_\_cms/  
\_\_\_\_\_\_engine/  
\_\_\_\_\_\_\_\_\_index.php  
\_\_\_\_\_\_webroot/  
\_\_\_\_\_\_\_\_\_images/  
\_\_\_\_\_\_\_\_\_css/  
others/

So the folder “cms” is placed in a webroot of the main site. The point  
is that all queries like “/js/jquery.js” must be treated as  
“/webroot/js/jquery.js” and all cms queries like “/cms/js/jquery.js”  
must be treated as “/webroot/cms/webroot/js/jquery.js”. I spent 2 days  
writing a proper config, the main problem is that all queries to /cms/  
location MUST NOT be parsed in “location / {…}” and all queries in  
“location /cms/ {…}” must not be parsed in “location / {…}”

Here is my config:

server {  
listen 8080;  
server\_name glinka.fm;  
root /home/renat/www/glinka;  
index index.php index.html;

```
# rules for all queries except /cms/ (how to do that??) "location ~

```

/(?!cms)" makes a cycle and 500 error  
location / {  
try\_files /webroot/$uri /engine/index.php;  
}

```
# rules for /cms/ queries only
location /cms/ {

```

rewrite /cms/(._) /webroot/cms/webroot/$1;  
if (!-f $request\_filename) {  
rewrite (._) /webroot/cms/engine/index.php;  
}  
}

```
location ~ \.php$ {
  include fastcgi.conf;
}

```

}

Please help me

Posted at Nginx Forum:

> **[Match any location except particular one](https://forum.nginx.org/read.php?2,218819,218819#msg-218819)**
>
> Hello, i have a website with the following files structure: engine/ \_\_index.php webroot/ \_\_images/ \_\_css/ \_\_cms/ \_\_\_\_\_\_engine/ \_\_\_\_\_\_\_\_\_index.php \_\_\_\_\_\_webroot/ \_\_\_\_\_\_\_\_\_images/ \_\_\_\_\_\_\_\_\_css/ others/

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [November 23, 2011, 3:46pm UTC](https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161/2 "2011-11-23T15:46:23Z")

</div>

On Wed, Nov 23, 2011 at 08:27:55AM -0500, mennanov wrote:

Hi there,

> I spent 2 days  
> writing a proper config, the main problem is that all queries to /cms/  
> location MUST NOT be parsed in “location / {…}” and all queries in  
> “location /cms/ {…}” must not be parsed in “location / {…}”
> 
> Here is my config:

You have three top-level location{} blocks here.

You probably only want two.

> ```
> location / {
> 
> ```

This will match all requests that don’t match another location.

> ```
> # rules for /cms/ queries only
> location /cms/ {
> 
> ```

This will match all requests that begin “/cms/”, unless they match  
another location.

> ```
> location ~ \.php$ {
> 
> ```

This will match all requests that end in “.php”. Specifically including  
both “/index.php” and “/cms/index.php”. Which is likely not what you  
want.

In nginx, every request is handled by one location{} block.

See, for example, [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpCoreModule#location) for  
details of how the one is chosen.

So you probably want something like

location / {

# all “location /” config

location ~ .php$ {  
# all “php inside location /” config  
}  
}

location /cms/ {

# all “location /cms/” config

location ~ .php$ {  
# all “php inside location /cms/” config  
}  
}

Good luck with it,

## f

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

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [November 23, 2011, 4:11pm UTC](https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161/3 "2011-11-23T16:11:44Z")

</div>

Thanks but i’ve already found a solution in the russian nginx forum.

location ~ ^/cms/(.\*)$ {  
try\_files /webroot/cms/webroot/$1 /webroot/cms/engine/index.php;  
}

Is what i needed.

Posted at Nginx Forum:

> **[Re: Match any location except particular one](https://forum.nginx.org/read.php?2,218819,218849#msg-218849)**
>
> Hello, i have a website with the following files structure: engine/ \_\_index.php webroot/ \_\_images/ \_\_css/ \_\_cms/ \_\_\_\_\_\_engine/ \_\_\_\_\_\_\_\_\_index.php \_\_\_\_\_\_webroot/ \_\_\_\_\_\_\_\_\_images/ \_\_\_\_\_\_\_\_\_css/ others/

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [November 23, 2011, 5:16pm UTC](https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161/4 "2011-11-23T17:16:46Z")

</div>

On Wed, Nov 23, 2011 at 10:11:08AM -0500, mennanov wrote:

> Thanks but i’ve already found a solution in the russian nginx forum.
> 
> location ~ ^/cms/(.\*)$ {  
> try\_files /webroot/cms/webroot/$1 /webroot/cms/engine/index.php;  
> }
> 
> Is what i needed.

location /cms/ {  
alias /home/renat/www/glinka/webroot/cms/webroot/;  
try\_files $uri /webroot/cms/engine/index.php;  
}

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [November 23, 2011, 5:26pm UTC](https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161/5 "2011-11-23T17:26:40Z")

</div>

## Igor S. Wrote:

> > Is what i needed.  
> > Igor S.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

Is there any way to use relative path in alias?  
It does not look comfortable to write the whole absolute path, it would  
be rather better if it is current root + path

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

---

<div class="post-metadata">

**Author:** ![mennanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/45deac/32.png) [@mennanov](https://www.ruby-forum.com/u/mennanov)\
**Post date:** [November 23, 2011, 5:29pm UTC](https://www.ruby-forum.com/t/match-any-location-except-particular-one/214161/6 "2011-11-23T17:29:46Z")

</div>

On Wed, Nov 23, 2011 at 11:26:02AM -0500, mennanov wrote:

> > > }
> 
> be rather better if it is current root + path  
> alias is more flexible root.

–  
Igor S.
