Nginx – Partial reverse http proxy using nginx

apache-2.2configurationnginxreverse-proxy

I've got several HTTP services running on the same machine, on different ports. I'd like to use nginx as a reverse proxy, but I can't seem to get my setup quite right.

I'd like the following:

  • /fossil/ ==> http://127.0.0.1:8080/fossil/index.php
  • /fossil/(whatever) ==> http://127.0.0.1:8080/(whatever)
  • /webmin/ ==> http://127.0.0.1:10000/
  • a few more specific locations to be served by nginx itself
  • and everything else to be handled by Apache ==> http://127.0.0.1:8001

It's the first two that seem to cause trouble; I want everything under /fossil/ to be handled by fossil, on port 8080; except the root itself, that one has to be handled by a special PHP page (under Apache).

What would be the way to go here?

Best Answer

Try the below config.

Be sure to see the comments in the location = /fossil/ section. Also keep in mind that requests to /fossil/(whatever) become /(whatever), so any urls returned in your content should be /fossil/(whatever) and not /(whatever). If necessary you could use sub_filter on the nginx side to substitute /fossil/(whatever) for /(whatever) when the content is return to the client.

location = /fossil/ {
  # matches /fossil/ query only
  #
  # if Apache isn't configured to serve index.php as the index
  # for /fossil/ uncomment the below rewrite and remove or comment
  # the proxy_pass
  #
  # rewrite /fossil/ /fossil/index.php;
  proxy_pass http://127.0.0.1:8080;
}

location = /fossil/index.php {
  # matches /fossil/index.php query only
   proxy_pass http://127.0.0.1:8080;
}

location /fossil/ {
  # matches any query beginning with /fossil/
  proxy_pass http://127.0.0.1:8080/;
}

location /webmin/ {
  # matches any query beginning with /webmin/
  proxy_pass http://127.0.0.1:10000/;
}

location / {
  # matches any query, since all queries begin with /, but regular
  # expressions and any longer conventional blocks will be
  # matched first.
  proxy_pass http://127.0.0.1:8001;
}

# locations to be handled by nginx go below
Related Topic