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 little awkward setup with a Apache 2 webserver being the gatekeeper and proxying traffic based on the incoming domain:

<VirtualHost *:80>
  ServerName example.org

  RewriteEngine On
  RewriteRule ^/(.*) http://localhost:3000/$1 [P]
</VirtualHost>

Running under localhost:3000 I have a lighttpd running which serves static content and acts as a reverse proxy for two other services, running on port 3001 and 3002. The configuration looks like this:

# lighttpd will also be used to forward requests to node.js
server.modules = (
  "mod_proxy"
)

# This config is meant to work relative to certain directories,
# it is not meant to be used somewhere globally in /etc/
var.projectRoot = var.CWD

# Setting up paths
server.document-root = var.projectRoot + "/client"

# Port to listen on
server.port          = 3000

# We don't want to provide names of individual files
index-file.names = ( "index.html" )

# Serve all API calls to the API server
$HTTP["url"] =~ "^/api/.*$" {
  proxy.server  = ( "" => (
      ( "host" => "127.0.0.1", "port" => 3001 )
    )
  )
}

# Serve all pages that are not known as static or api routes
# to the page instance
$HTTP["url"] !~ "^/(assets|game|ige|api).*$" {
  proxy.server  = ( "" => (
      ( "host" => "127.0.0.1", "port" => 3002 )
    )
  )
}

The whole setup works fine for the following cases:

  • GET /assets/textures/sources.txt serves a static file from the client directory.
  • GET /api/games/byId/123 retrives the correct data from the API server on port 3001
  • GET / retrieves the page from port 3002

However, the request GET /game (where the game folder has a index.html file) does get served, but changes the URL to http://localhost:3000/game. Or to be more precise: It changes to whatever URL I have specified in the Apache2 vhost, If I put example.org:3000 in there, it will attempt to serve example.org:3000/game, Ignoring the [P] proxy directive.

Things only go wrong when lighttpds "rewrites" to deliver folder/index.html files for folder/ requests. Can anybody tell me why?

The behaviour is the same for Chrome and Firefox and I don't get any HTTP redirects.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

It could be some redirect munging by the lighttpd server, since you don't have a ProxyPassReverse directive specified (it works with RewriteRule too). That said, I don't see anything about your configuration that specifically requires mod_rewrite to perform this proxy: it's best practice to avoid using it when there are dedicated directives to do the job.

Would you mind trying this instead?

<Location />
    ProxyPass        http://localhost:3000
    ProxyPassReverse http://localhost:3000
</Location>
share|improve this answer
    
Thanks alot, that did the trick. I never got around really "learning" how Apache2 works and just did some copypasta. –  Marcus Riemer Jan 14 '13 at 9:50
2  
I'd use ProxyPreserveHost On instead of ProxyPassReverse; ProxyPassReverse only fixes certain response headers, ProxyPreserveHost will fix all urls generated by the backend. Also it allows to you to use more than one vhost on the connection to the backend. I have no idea why this is not the default, because it is the only sane way imho. –  Stefan Jan 14 '13 at 11:59
    
Thanks for bringing that to my attention! –  Marcus Riemer Jan 14 '13 at 14:10
    
Likewise, it's been awhile. :) Thanks! –  Andrew B Jan 14 '13 at 14:57

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.