Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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
share|improve this answer
    
Thanks a lot, this one also works. I’ve accepted this answer because of the tip about sub_filter; fossil does assume that it’s sitting in the root of the server, so I need to rewrite the URLs in the page... –  Martijn Apr 21 '11 at 12:56

This is actually fairly straightforward with two types of static location. location = is an exact match, and location /location is a prefix match. http://wiki.nginx.org/HttpCoreModule#location

server {
  server_name www.example.com;

  # Set defaults for the proxy_pass directives in each location
  # Add the client IP
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  # Pass through the request hostname
  proxy_set_header Host $host;

  # Everything that doesn't match a more specific location
  location / {
    proxy_pass http://127.0.0.1:8001;
  }

  location = /fossil/ {
    proxy_pass http://127.0.0.1:8080/fossil/index.php;
  }

  location /fossil/ {
    # Do you really want to strip off /fossil here but not above?
    # The trailing / replaces /fossil/ with /
    proxy_pass http://127.0.0.1:8080/;
  }

  location /webmin/ {
    proxy_pass http://127.0.0.1:10000/;
  }

  .. add your other locations ...
}
share|improve this answer
    
Thanks a bunch, this seems to do the trick! I hadn’t found the = prefix. And yes, I do want to strip off fossil; I just made a mistake in my question: /fossil/ should redirect to Apache (on :8001), /fossil/* should redirect to the Fossil server (on :8080). It turns out, though, that Fossil wants to sit on the root path, so it doesn’t quite work yet. I guess I’ll have to use a subdomain for it... –  Martijn Apr 21 '11 at 12:53

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.