Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?
share|improve this question
    
For best practices - wiki.nginx.org/Pitfalls –  pkhamre Oct 24 '12 at 12:19
add comment

2 Answers 2

up vote 1 down vote accepted

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;
}
share|improve this answer
    
Thanks! Do you know how this would affect performance as I have quite a lot of these rewrites? –  j0nes Oct 25 '12 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 '12 at 16:39
    
Thanks, with this accept you get 10k reputation :) –  j0nes Oct 26 '12 at 8:26
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.