nginx中proxy_pass的常见匹配用法

本文详细介绍了Nginx中proxy_pass指令的不同用法及其如何影响请求的转发过程。通过六个具体示例展示了带有和不带URI时,请求路径的变化及如何被重写转发到后端服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代理转发规则

如果proxy_pass使用了URI,当传送请求到后端服务器时,规范化以后的请求路径与配置中的路径的匹配部分将被替换为指令中定义的URI(对应下文第一种情况)。

如果proxy_pass没有使用URI,传送到后端服务器的请求URI一般客户端发起的原始URI(下文第二种情况)。

访问地址:http://localhost:8080/abc/dfg

1

location /abc/ {
	proxy_pass http://127.0.0.1:8080/;
}

代理到:http://127.0.0.1:8080/dfg

2

location /abc{
	proxy_pass http://127.0.0.1:8080;
}

代理到:http://127.0.0.1:8080/abc/dfg

3

location /abc/ {
	proxy_pass http://127.0.0.1:8080/api/;
}

代理到:http://127.0.0.1:8080/api/dfg

4

location /abc/ {
	proxy_pass http://127.0.0.1:8080/api;
}

代理到:http://127.0.0.1:8080/apidfg

5

location /abc {
	proxy_pass http://127.0.0.1:8080/;
}

代理到:http://127.0.0.1:8080//dfg

6

location /proxy {
	proxy_pass http://127.0.0.1:8080;
}

代理到:http://127.0.0.1:8080/abc/dfg

7

location /abc/dfg {
	proxy_pass http://127.0.0.1:8080/mno/xyz;
}

代理到:http://127.0.0.1:8080/mno/xyz

其他的匹配规则,可以看看别的

另附nginx官方文档链接和阿里Tengine中文版链接
链接: https://blog.redis.com.cn/doc/standard/httpproxy.html
链接: http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_proxy_module.html#proxy_pass

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值