我在同一台机器的不同端口上运行了多个 HTTP 服务。我想使用 nginx 作为反向代理,但我似乎无法正确设置。
我想要以下内容:
/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/
- nginx 自身提供的几个更具体的位置
- 其余一切都由 Apache 处理 ==>
http://127.0.0.1:8001
前两个似乎会引起麻烦;我希望下面的所有内容/fossil/
都由fossil在端口8080上处理;除了根本身,那个必须由特殊的PHP页面(在Apache下)处理。
去这里该怎么走?
答案1
尝试以下配置。
请务必查看本节中的注释location = /fossil/
。另外请记住,对 /fossil/(whatever) 的请求将变为 /(whatever),因此内容中返回的任何 URL 都应为 /fossil/(whatever) 而不是 /(whatever)。如有必要,您可以在 nginx 端使用 sub_filter 在将内容返回到客户端时将 /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
答案2
这实际上相当简单,有两种类型的静态位置。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 ...
}