3

The Apache documentation pretty clearly states that mod_rewrite should only be a last resort. For nginx, the Pitfalls section has examples where the request_uri is essentially unchanged.


The situation is this:

  • We (a department in an institute) have a main webserver which is due to be upgraded soon (running Debian 6). To deal with the upgrade, I have set up an nginx server, which will proxy parts known to be working fine to an upgraded server and others to the old server.
  • The old server had a number of ProxyPass rules (a significant amount of its work is proxying). I wish to shift all of them to nginx, since, as I understand, nginx is more performant in proxying.
  • A large number of users have user home directories being served by another server. For these users, but tilde and non-tilde versions are available (i.e., http://my.site/muru and http://my.site/~muru would be proxied to http://other.server/~muru, but http://other.server/muru doesn't exist).
  • A number of folders were proxied to a different folder on the same server. (e.g., http://my.site/local-club would be proxied to http://my.site/~local-club).

My main question is:

  • Given A and B both being proxied to C on a different server, should I use rewrite or location with return to redirect requests for B to A, or continue proxying both?
  • Given A and B both on the same server, with B being proxied to A, should I use rewrites or returns to redirect B to A?

Redirection (and return) has the benefit of clearly indicating the relation between the two directories.


My rewrite rules look like:

rewrite ^/B(/.*) /A$1 permanent;

And the return rules:

location ~ ^/B(/.*) {
    return 301 /A$1;
}

With:

location ~ ^/~(A|D|E|F..)/ {
    proxy_pass https://other.server;
    proxy_redirect default;
}
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
muru
  • 589
  • 8
  • 26

1 Answers1

3

That doesn't really matter execpt if your nginx server will deal with thousands of requests per second and you want to avoid each request to spend CPU time in rewrite regexs matching. Now, I would say use the most clear way to write your configuration files until you reach this limit then if you don't have the budget to scale your infrastructure, it would be time to think about tricking your configuration to fit your traffic. The drawback of return is that if you have multiple redirections you will end up with one location wrapping one return directive. Using rewrites you can wrap multiple of them in one generic location and test in the first parameter for a more specific pattern.

Update : an example of multiple redirection in a generic location :

location /B {
    rewrite ^/B/foo/(.*)$ /A/newfoo/$1 permanent;
    rewrite ^/B/bar/(.*)$ /A/newbar/$2 permanent;
    rewrite ^/B/(.*)$ /A/$1 permanent;
}
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
  • +1, thanks. I'll decide on the final choice after a week's test drive. I find the last line a bit unclear: "wrap multiple of them in one generic location". Can you provide an example? – muru Dec 03 '14 at 22:27
  • @muru I added an example in my answer. Basically in this example, when the beginning of the URI matches /B, rewrite rules are checked sequentially. If the regex of a rewrite rule doesn't match the current URI being processed then the next one is checked and so on. – Xavier Lucas Dec 03 '14 at 22:43
  • Ah, yes. I don't have an instance of that case in my configuration (if `/B` is redirected, that's the end. `/B/C` doesn't enter the picture anywhere else), but that can change. One last thing, I made a typo in my question, since corrected. Basically both `rewrite` and `return` are using regexes at some point. So does the performance difference vanish in that case? – muru Dec 03 '14 at 22:52
  • @muru In case you don't have other rewrites then there is no difference. The only difference comes in the case of my example, which is not yours for the moment, where you could end up processing several rewrites for every request matching an URI pattern while your could spare this CPU time with a returns wrapped in specific locations. – Xavier Lucas Dec 03 '14 at 23:04