Nginx学习——proxy_pass

参考官网:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

定义:用来设置被代理服务器的协议(http或https)和地址(域名或者IP地址加端口),还可以设置可选的URI

例:proxy_pass http://localhost:8000/uri/

URI按如下规则传送给后端被代理服务器:

客户端请求以http://127.0.0.2:8080/test/xx/yy....为例被监听转发

1、如果proxy_pass使用了URI(就是有/),请求路径与loction路径的匹配部分将被 替换 为proxy_pass中定义的URI

location /test/{
  proxy_pass http://127.0.0.3/service/test1/;
}
转发以后的请求为http://127.0.0.3/service/test1/xx/yy...

2、如果proxy_pass没有使用URI,发给被代理服务器的请求路径和客户端发起的请求路径相同,不会被修改

location /test/ {
  proxy_pass http://127.0.0.3/service/test1;
}
转发以后的请求为http://127.0.0.3/service/test1/test/xx/yy...

特殊情况:

1、location 使用了正则表达式,这种情况下指令不应该带有URI。

2、使用rewrite指令改变了URI,并使用相同配置处理请求(break):

location /name/ {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1;
}

这种情况下,指令定义的URI将被忽略,改变后的URI将被发送给后端服务器。

posted @ 2019-12-11 16:46  鼠标的博客  阅读(330)  评论(0编辑  收藏  举报