使用nginx的部分反向http代理

我有几个HTTP服务运行在同一台机器上,在不同的端口上。 我想使用nginx作为反向代理,但我似乎无法让我的设置非常正确。

我想要以下内容:

  • /fossil/ ==> http://127.0.0.1:8080/fossil/index.php
  • /fossil/(whatever) ==> http://127.0.0.1:8080/(whatever) /fossil/(whatever) http://127.0.0.1:8080/(whatever)
  • /webmin/ ==> http://127.0.0.1:10000/
  • 一些更具体的位置由nginx自己服务
  • 和其他一切由Apache ==> http://127.0.0.1:8001处理

这是头两个似乎造成麻烦, 我希望/fossil/下的所有东西都能在8080号港口化石处理。 除了根本身,必须由一个特殊的PHP页面(在Apache下)来处理。

怎么走这里?

尝试下面的configuration。

一定要在location = /fossil/ section中看到注释。 还要记住,要求/化石/(无论)成为/(无论),所以在你的内容返回的任何url应该是/化石/(不pipe),而不是/(不pipe)。 如果有必要的话,你可以在内容返回给客户端的时候使用nginx端的sub_filterreplace/ fossil /(whatever)作为/(whatever)。

 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 

这实际上相当简单,有两种types的静态位置。 location =完全匹配,location / location是前缀匹配。 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 ... }