How to optimize these config / rewrite rules

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:
http://forum.nginx.org/read.php?2,225483,225483#msg-225483

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

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,225483,225484#msg-225484

On 18 April 2012 15:53, rob [email protected] 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
http://www.jpluscplusm.com/contact.html

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:
http://forum.nginx.org/read.php?2,225483,225487#msg-225487

On 18 April 2012 16:48, rob [email protected] 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 :wink:

  2. Regex locations are, as per
    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 :slight_smile:
#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
http://www.jpluscplusm.com/contact.html

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 :slight_smile:

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,225483,225492#msg-225492