1

I have a website where I need to change the URL structure. The old URLs look like /olddir/part1_de.htm, the new ones will look like /newdir/sub/category/anotherpage.htm. There are a lot of URL rewrites I need to do, I assume about 500 distinct rewrites in the end.

As my website gets quite a lot of traffic, my main concern is about performance at the moment. My questions are:

  • I assume that for each request, the rewrites block will be parsed and the regex will be evaluated. Am I right?
  • Will there be a performance penalty if I use these rewrites? Can nginx handle this?
  • Are there any "best practices" to follow when doing a lot of rewrites?
1

2 Answers 2

2

You may find the map module to perform better, especially if most of the URLs don't actually need to be regular expressions. For example (not tested):

map $uri $new {
    /olddir/part1_de.htm    /newdir/sub/category/anotherpage.htm;
    ~^/olddir/part(?<num>[0-9]+)_en.htm$    /newdir/sub/category/part$num;
    ...
}

if ($new) {
    rewrite ^ $new permanent;
}
2
  • Thanks! Do you know how this would affect performance as I have quite a lot of these rewrites?
    – j0nes
    Oct 25, 2012 at 7:35
  • @j0nes I've never investigated the performance implications. I would guess that it's equivalent to the same number of rewrite directives if they're all regular expressions, but faster if most of them are not regular expressions (i.e. plain strings).
    – mgorven
    Oct 25, 2012 at 16:39
1

Nginx compiles its configuration into some sort of binary tree during initial start or configuration reload (not with each request). So it is OK to use rewrites, but recommended option and best practice is to use try_files directive when possible.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .