1、 rewrite
指令。
location /test {
rewrite ^/test(.*)$ $1 break;
proxy_pass http://对应的地址;
}
-
location /test
:指定匹配请求路径为/test
。 -
rewrite ^/test(.*)$ $1 break;
:使用正则表达式将/test
后面的内容捕获到$1
变量中。^/test(.*)$
表示以/test
开头的路径,并捕获后续的任意字符。 -
proxy_pass http://对应的地址;
:使用proxy_pass
指令将请求转发到对应的地址。将对应的地址
替换为你要转发到的目标地址。
通过以上配置,当请求路径为 /test/some/path
时,Nginx 将会将路径 /some/path
作为新的请求路径,并转发到对应的地址。
2、proxy_pass指令
location /test {
proxy_pass http://对应的地址$uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
-
location /test
:指定匹配请求路径为/test
。 -
proxy_pass http://对应的地址$uri;
:使用proxy_pass
指令将请求转发到对应的地址,并附加上原始请求中的 URI 部分(即去掉/test
后的内容)。 -
proxy_set_header Host $host;
:设置请求头的 Host 字段为客户端发送的原始 Host 字段。 -
proxy_set_header X-Real-IP $remote_addr;
:设置请求头的 X-Real-IP 字段为客户端的真实 IP 地址。
通过以上配置,当请求路径为 /test/some/path
时,Nginx 将会将请求转发到 http://对应的地址/some/path
。