12

What is the difference between using mod_proxy and mod_rewrite?

I have a requirement to send certain url patterns through the tomcat, which runs on the same host but under port 8080. I know this is something for mod_proxy, but I"m wondering why I can't just use mod_rewrite, or what the difference is?

Probably has to do w/ reverse proxy, and also when in the pipeline it gets handled?

Thanks.

Scott Klarenbach
  • 559
  • 2
  • 8
  • 19

2 Answers2

7

mod_rewrite using the P flags puts the request through mod_proxy. The advantage in using mod_rewrite is that you get more control for mapping requests, just like rewrite let's you rewrite URLs. But the proxying is exactly the same. The disadvantage is that mod_rewrite syntax is more complex. So my recommendation is to use mod_proxy -style configuration directives unless you need something more complicated. Others will probably recommend mod_rewrite -style, because then you only have to learn one style.

ptman
  • 27,124
  • 2
  • 26
  • 45
  • As ptman already mentioned, with mod_rewrite you can do crazy stuff like dynamic routing to a proxy. This is sometimes very handy and you can connect to a proxy-cluster with one command instead of multiple mod_proxy lines. But is more complex and sometime not easy to debug. – DrDol Sep 25 '10 at 10:15
  • 1
    According to [the Apache documentation](https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_p), performance will be better using mod_proxy directly with the [ProxyPass](https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass) or [ProxyPassMatch](https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassmatch) directives. – Stack Underflow Sep 15 '21 at 22:30
4

mod_rewrite is very general and very powerful: it can handle not only proxying but also aliasing, redirection, and pretty much any sort of custom mapping of URLs to either other URLs or filenames. (Well, I suppose if you looked hard enough you could find something mod_rewritecan't do) But there are several potential reasons not to use it:

  • You might not want to deal with the complexity of mod_rewrite. It is notoriously tricky to configure properly - the configuration is practically a programming language in itself.
  • Apache takes time to process all those complex rewriting rules, and if you have a busy server that may be something you don't want to deal with. Using mod_proxy directly allows you to invoke more streamlined code that might help your server run faster. (Honestly, I'm not sure how large of an effect this is, but I don't think it's that major)
David Z
  • 5,376
  • 2
  • 24
  • 22