# Nginx RP: a lot of rewrite rules

**URL:** <https://www.ruby-forum.com/t/nginx-rp-a-lot-of-rewrite-rules/227946>\
**Category:** NGINX\
**Created:** [April 4, 2013, 9:02am UTC](https://www.ruby-forum.com/t/nginx-rp-a-lot-of-rewrite-rules/227946 "2013-04-04T09:02:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davide\_DSAmico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Davide\_DSAmico](https://www.ruby-forum.com/u/Davide_DSAmico)\
**Post date:** [April 4, 2013, 9:02am UTC](https://www.ruby-forum.com/t/nginx-rp-a-lot-of-rewrite-rules/227946/1 "2013-04-04T09:02:00Z")

</div>

Hi,  
I’ve two freebsd 9.1 vm (2 corees, 4gb ram) with nginx-1.2.7 in  
active/passive mode (using carp), acting as a reverse proxy to 4  
apache22 backend (2 cores, 4gb ram).  
I should set 800 simple rewrite rules, such as:

rewrite ^/something/foo.html /bar.html permanent;

I cannot use any regexp to optimize all these rules, I have no  
scripting language enabled (the vhosts will serve only static pages, the  
apache22 backend will use SSI too).

Here the question:

Is it better to use (in terms of performances, reliability, load on the  
reverse proxy) the rewrite rules on the nginx reverse proxy, or use the  
rewrite rules on the apache22 backend?

Thanks,  
d.

---

<div class="post-metadata">

**Author:** ![Davide\_DSAmico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Davide\_DSAmico](https://www.ruby-forum.com/u/Davide_DSAmico)\
**Post date:** [April 4, 2013, 11:35am UTC](https://www.ruby-forum.com/t/nginx-rp-a-lot-of-rewrite-rules/227946/2 "2013-04-04T11:35:07Z")

</div>

On Thu, Apr 04, 2013 at 09:01:17AM +0200, Davide D’Amico wrote:

Hi there,

> Is it better to use (in terms of performances, reliability, load on the  
> reverse proxy) the rewrite rules on the nginx reverse proxy, or use the  
> rewrite rules on the apache22 backend?

Completely untested, but it seems that “the earliest place you can do  
it”  
is the right place to do it, at least for a straightforward mechanical  
transformation like this.

So I’d say “on nginx”.

When you are doing the load-testing, you might want to consider  
comparing:

- rewrite ^/old /new permanent;
- location = /old { return 301 /new; }
- map new $uri { default 0; /old /new; }  
if ($new) { return 301 /new; }

(Confirm the syntax before testing; these are just some alternative  
suggestions.)

I suspect that the suggestions are increasingly more efficient – but  
you presumably have the interest to find out for sure, on your hardware  
and with your expected loads.

Cheers,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Davide\_DSAmico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Davide\_DSAmico](https://www.ruby-forum.com/u/Davide_DSAmico)\
**Post date:** [April 4, 2013, 12:21pm UTC](https://www.ruby-forum.com/t/nginx-rp-a-lot-of-rewrite-rules/227946/3 "2013-04-04T12:21:18Z")

</div>

Il 04/04/13 11:34, Francis D. ha scritto:

> transformation like this.  
> (Confirm the syntax before testing; these are just some alternative  
> suggestions.)
> 
> I suspect that the suggestions are increasingly more efficient – but  
> you presumably have the interest to find out for sure, on your hardware  
> and with your expected loads.

Thank you Francis, but I cannot “group” all the rewrite I have so I am  
starting using all these rewrites on backends (where I have rewritemaps,  
too) and later I’ll test them on nginx.

d.

---

<div class="post-metadata">

**Author:** ![Davide\_DSAmico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Davide\_DSAmico](https://www.ruby-forum.com/u/Davide_DSAmico)\
**Post date:** [April 4, 2013, 8:19pm UTC](https://www.ruby-forum.com/t/nginx-rp-a-lot-of-rewrite-rules/227946/4 "2013-04-04T20:19:58Z")

</div>

On Thu, Apr 04, 2013 at 12:20:42PM +0200, Davide D’Amico wrote:

> Il 04/04/13 11:34, Francis D. ha scritto:
> 
> > On Thu, Apr 04, 2013 at 09:01:17AM +0200, Davide D’Amico wrote:

Hi there,

> > I suspect that the suggestions are increasingly more efficient – but  
> > you presumably have the interest to find out for sure, on your hardware  
> > and with your expected loads.
> 
> Thank you Francis, but I cannot “group” all the rewrite I have so I am  
> starting using all these rewrites on backends (where I have rewritemaps,  
> too) and later I’ll test them on nginx.

I may have been unclear. I’m not talking about grouping the rewrites;  
I’m talking about a list of desired old → new redirections, which  
presumably you have somewhere.

Get the /oldN → /newN local urls that you care about, and in nginx you  
can try (and I have now tested this):

===  
http {

map $uri $new {  
default “”;  
/old3 /new3;  
/old4 /new4;  
}

server {  
listen 8000;

```
if ($new) {
  return 301 $new;
}

location = /old1 { return 301 /new1 ;}
location = /old2 { return 301 /new2 ;}

```

}

# }

and look at the output of things like

curl -I [http://localhost:8000/old1](http://localhost:8000/old1)

Test twice, once with many “location =” lines and no map/if; and the  
other  
time with many lines inside the map and no special “location =” lines;  
in order to know which is better on your system.

Anyway, if you have something working well enough for you now, then you  
don’t need to change anything. But when the time comes, this is a more  
specific example of what I was suggesting.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
