Bug #164

mod_proxy and mod_rewrite inside the same URL conditional?

Added by Anonymous about 15 years ago. Updated over 2 years ago.

Target version:


$HTTP["url"] =~ "^/projects/cs(.*)" {
    url.rewrite = ("^/projects/cs(.*)" => "$1")
    proxy.server = ("/" => (( "host" => "", "port" => 9090 )))

In the case of a conditional, as above, the request is proxied but is never rewritten.

url.rewrite = ("^/projects/cs(.*)" => "$1")
proxy.server = ("/" => (( "host" => "", "port" => 9090 )))

In this case (no conditional), the request is rewritten and then proxied, as expected.

-- lighttpd


lighttpd-mod-rewrite-host-test.patch (2.82 KB) lighttpd-mod-rewrite-host-test.patch "url.rewrite" is deprecated: use url.rewrite-once instead conny, 2006-02-09 11:37

Updated by jan about 15 years ago

  • Status changed from New to Assigned

Updated by Anonymous over 14 years ago

I'm having this problem too. See

-- ryan at art of mission dot com


Updated by conny over 14 years ago

I created a pair of extra testcases for "url.rewrite inside $HOST[]".


Updated by conny over 14 years ago

But wait a moment... The first case described in the ticket is a bit odd nevertheless:

You rewrite "!^/projects/csFOO" to "/FOO", after which there are two possible outcomes:

  1. After the rewrite we're "back at the start" so to say: the consecutive pass does NOT match the $HOSTurl specified ("/FOO" !~ ""!^/projects/cs(.*)" and therefore handled by the outside context instead (not proxied).
  1. Proxied requests are always handled directly after rewrites. So in this case "/FOO/" is proxied to ""

The current documentation does not specify which case is supposed to happen.


Updated by conny over 14 years ago

With neither clear documentation nor a test unit for mod_proxy available at the moment: just stick to the alternative without conditionals.

It is really confusing to say in effect "for stuff matching /FOO: proxy everything to that matches /*everything*". Huh?!


Updated by Anonymous about 14 years ago

re: using the alternative without conditionals, there is value in doing it the first way.

Say I want to serve normal html pages but all requests to /myappserver need to be redirected.

Now say that the app server wants a clean URL, e.g. instead of /myappserver/foo going to the app server it's /foo.

In apache this functionality is achieved by

RewriteRule ^myappserver/(.*)$  http://localhost:2323/$1 [P]

There are work arounds but it would be nice to map urls cleanly.

-- weeksie


Updated by jan about 13 years ago

  • Status changed from Assigned to Fixed
  • Resolution set to fixed

such rewrites are supported by mod-proxy-core in 1.5.0 with proxy-core.rewrite-request.


Updated by cstefkivoila over 11 years ago

jan wrote:

such rewrites are supported by mod-proxy-core in 1.5.0 with proxy-core.rewrite-request.


I have the same problem than above.
Indeed, I want to achieve the following Apache line with lighttpd:
RewriteRule ^myappserver/(.*)$ http://localhost:2323/$1 [P]

I am currently using lighttpd 1.4.20 on a DNS-323.
It is technically difficult to upgrade my lighttpd to the 1.5.0 release.
So my question is:
"Is there a work around of this problem for a 1.4 release of lighttpd?"

Thanks in advance for your help!


Updated by stefan over 8 years ago


here is a workaroud I used to map an web-application on http://localhost:8008/ (its contextroot wasn't changable by configuration)
to http://webserver/myapp. The idea behind this was to do the proxying and URL rewriting inside a separate lighttpd-instance
listening on a port not being port 80 (here: port 81). This separate instance is then accessed by a lighttpd instance listening on port 80 and using
mod_proxy. Here is the config I used:

# Part for proxy listening on port 80
$HTTP["url"] =~ "(^/webapp/)" {
  proxy.server  = ( "" => (
    "webapp:80" => # name
      ( "host" => "",
        "port" => 81

# Part for proxy listening on port 81
$SERVER["socket"] == ":81" {
  url.rewrite-once = ( "^/webapp/(.*)$" => "/$1" )
  proxy.server  = ( "" => (
    "webapp:81" => # name
      ( "host" => "",
        "port" => 8008

Hope this helps someone.


Updated by flynn almost 4 years ago

Although I used this "double" proxy solution for many years, I searched for better solution.

Here is my new approach with lua/mod_magnet:

$HTTP["url"] =~ "^/webapp/" {
  magnet.attract-raw-url-to = ( "/etc/lighttpd/lua/webapp.lua" )
  proxy.server = ( "" => ("webapp" => ("host" => "", "port" => 8008)))

and /etc/lighttpd/lua/webapp.lua:

lighty.env["request.uri"] = string.sub(lighty.env["request.uri"], string.len('/webapp/'))

This is very close to the original request ...


Updated by gstrauss over 2 years ago

  • Description updated (diff)

FYI: lighttpd 1.4.46 introduces proxy.header which performs limited URL-prefix rewriting. More info at Docs_ModProxy

Also available in: Atom