# How to optimize these config / rewrite rules

**URL:** <https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012>\
**Category:** NGINX\
**Created:** [April 18, 2012, 4:54pm UTC](https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012 "2012-04-18T16:54:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [April 18, 2012, 4:54pm UTC](https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012/1 "2012-04-18T16:54:17Z")

</div>

Hi there,

I’m running nginx and xenforo but need to rewrite some old legacy URL’s  
(left over from vbulletin + vbseo)…

Here is just a very small selection of my config which I want to  
optimise as fully as possible:-

[code]  
#Forum 26 main  
location = /category-name-73/forum-name-26/ {  
rewrite ^ /forums/forum-name.26/ permanent;  
}

#Forum 26  
location /category-name-73/forum-name-26/ {  
#forum paging  
rewrite ^/category-name-73/forum-name-26/index([\d]+).html  
/forums/forum-name.26/page-$1 permanent;

```
#thread paging (modified version of kier's redirect script)
rewrite [^/]+/([\d]+)-.+-([\d]+).html /showthread.php?t=$1&page=$2

```

last;

```
#thread
rewrite [^/]+/([\d]+)-.+.html /threads/redirectme.$1/ permanent;

```

}[/code]

If only I can optimise the above then I can transfer the same principle  
to the rest of my (huge) config file.

I’ve been told that its possible to optimise the above so that a visit  
to /category-name-73/forum-name-26/ or any path below it will run just  
one regex instead of potentially 4 as per the above code…

So, optimising the above… does anyone have any clue how to do this?

Thank you for any help offered,

Rob

Posted at Nginx Forum:

> **[How to optimize these config / rewrite rules](https://forum.nginx.org/read.php?2,225483,225483#msg-225483)**
>
> Hi there, I'm running nginx and xenforo but need to rewrite some old legacy URL's (left over from vbulletin + vbseo).. Here is just a very small selection of my config which I want to optimise as fully as possible:- \[code\] #Forum 26 main location =

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [April 18, 2012, 4:55pm UTC](https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012/2 "2012-04-18T16:55:31Z")

</div>

bah… please ignore the `` tags above - assumed this forum  
software used bbcode.

Posted at Nginx Forum:

> **[Re: How to optimize these config / rewrite rules](https://forum.nginx.org/read.php?2,225483,225484#msg-225484)**
>
> Hi there, I'm running nginx and xenforo but need to rewrite some old legacy URL's (left over from vbulletin + vbseo).. Here is just a very small selection of my config which I want to optimise as fully as possible:- \[code\] #Forum 26 main location =

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [April 18, 2012, 5:38pm UTC](https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012/3 "2012-04-18T17:38:27Z")

</div>

On 18 April 2012 15:53, rob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[snip]

> So, optimising the above… does anyone have any clue how to do this?

Optimising for what?  
Runtime speed? Runtime memory use? Administrative efficiency?  
Administrative clarity? Future flexibility?

## J

Jonathan M.  
Oxford, London, UK

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

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [April 18, 2012, 5:49pm UTC](https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012/4 "2012-04-18T17:49:36Z")

</div>

As I said Jonathan, I’ve been told that the above rules can be  
optimised so that hitting any url in the path I mentioned would run just  
one rex rule instead of a minimum of 3 currently. I have been told that  
by an nginx expert but what I have not been told is how to achieve it. I  
suspect it will be by nesting location {} constructs (is thsat possible)  
or by having more selective location {} constructs.

I want to start with as optimised a set of rewrite rules as possible.

Does this help any?

Posted at Nginx Forum:

> **[Re: How to optimize these config / rewrite rules](https://forum.nginx.org/read.php?2,225483,225487#msg-225487)**
>
> Hi there, I'm running nginx and xenforo but need to rewrite some old legacy URL's (left over from vbulletin + vbseo).. Here is just a very small selection of my config which I want to optimise as fully as possible:- \[code\] #Forum 26 main location =

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [April 18, 2012, 6:52pm UTC](https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012/5 "2012-04-18T18:52:11Z")

</div>

On 18 April 2012 16:48, rob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> As I said Jonathan, I’ve been told that the above rules can be  
> optimised so that hitting any url in the path I mentioned would run just  
> one rex rule instead of a minimum of 3 currently. I have been told that  
> by an nginx expert but what I have not been told is how to achieve it. I  
> suspect it will be by nesting location {} constructs (is thsat possible)  
> or by having more selective location {} constructs.
> 
> I want to start with as optimised a set of rewrite rules as possible.
> 
> Does this help any?

I still don’t think you’ve defined the optimisation you’re looking  
for. Just to say “optimised” isn’t specific enough IMHO. You optimise  
/for/ a characteristic, almost always at the expense of other  
characteristics.

It looks to me like you’re trying to optimise for as few lines as  
possible in your config file, at the expense of config file _clarity_  
and runtime performance.  
Also, just because you can technically combine multiple regular  
expressions into a single one doesn’t mean that the result will be any  
faster than the multiple, simpler, regex route.

I can’t personally be bothered to work out the complex regex that  
would be required here for rewrites as it’s _really_ not something I’d  
do with my nginx configs.  
I want to understand them later and not worry if every minor change  
might have some huge unintended impact - which is where I think you’ll  
end up if you carry on down this route.  
However there are a few points I can give you, looking at what you’ve  
got.

1. Locations can be nested. This is valid:

location /foo {  
location /foo/bar {  
…  
}  
}

AFAICT, the intended use of this construct is to have non-regex outer  
locations, preventing the evaluation of inner regex locations until  
they’re necessary e.g.

location /foo/ {  
location ~ ^/foo/(bar|baz) {  
…  
}  
location ~ ^/foo/image(s)?/ {  
…  
}  
}

1. If you’re using brackets solely for alternation (i.e. one thing OR  
the other, e.g. “(bar|baz)”, above) then consider using (?:bar|baz)  
instead, as this stops the regex engine from capturing the match for  
later use, which might save you valuable microseconds 😉

2. Regex _locations_ are, as per  
[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), evaluated in order,  
stopping after the first match. This probably isn’t the case with  
rewrites, which are cumulative I believe.

3. Here’s how I’d structure what you’re doing if my aim was to make it  
forum-name agnostic. YMMV, 'cos it actually depends on the format  
names of the forums. You may need to tweak it.  
I wouldn’t personally do this unless I _really_ needed to make it  
name-agnostic. It sacrifices readability for brevity.

* * *

location ~ ^/category-name-73/(?[^/]+)/$ {  
rewrite ^ /forums/$forum/ permanent;  
}  
location ~ ^/category-name-73/(\<?forum\>[^/]+)/index(?[\d]+).html  
{  
rewrite /forums/$forum/page-$number permanent;  
}

# Now /you/ redo these as locations; to do so requires a bit more

## detail about the URI structures than you’ve posted 🙂 #rewrite [^/]+/([\d]+)-.±([\d]+).html /showthread.php?t=$1&page=$2 last; #rewrite [^/]+/([\d]+)-.+.html /threads/redirectme.$1/ permanent;

## HTH, Jonathan

Jonathan M.  
Oxford, London, UK

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

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [April 18, 2012, 7:14pm UTC](https://www.ruby-forum.com/t/how-to-optimize-these-config-rewrite-rules/219012/6 "2012-04-18T19:14:29Z")

</div>

Hi Jonathan,

That was exactly the advice I needed. I will spend some time rewriting  
what I think to be a better way based on your advice and post back here  
with more clearly defined rules using location a bit more.

Thanks 🙂

Posted at Nginx Forum:

> **[Re: How to optimize these config / rewrite rules](https://forum.nginx.org/read.php?2,225483,225492#msg-225492)**
>
> Hi there, I'm running nginx and xenforo but need to rewrite some old legacy URL's (left over from vbulletin + vbseo).. Here is just a very small selection of my config which I want to optimise as fully as possible:- \[code\] #Forum 26 main location =
