nginx proxy 详解,代理路径的转发

1.proxy_pass 为url时,没有/,当命中规则时,会用请求的 ip/域名+port 替换为proxy_pass指定的值去访问资源

server {
		listen 80;
		server_name  localhost;

		location /api {
			add_header Cache-Control no-cache;
			add_header Pragma no-cache;
			add_header Expires 0;
			# 动态页面
			proxy_pass  http://localhost:8080;
			
		}
	 }

请求路径: http://localhost:80/api/param/get1
实际处理后的请求链接为: http://localhost:8080/api/param/get1
http://localhost:80 替换为了 http://localhost:8080

2.proxy_pass 为某个具体uri时。例如http://localhost:8080/ 或者 http://localhost:8080/xxx。
那么当命中规则时,会把请求url的 ip/域名+port+匹配到的路径替换为proxy_pass指定的值去访问资源

server {
		listen 80;
		server_name  localhost;

		location /api {
			add_header Cache-Control no-cache;
			add_header Pragma no-cache;
			add_header Expires 0;
			# 动态页面
			proxy_pass  http://localhost:8080/;
			
		}
	 }

请求路径: http://localhost:80/api/param/get1
nginx实际处理后请求链接为: http://localhost:8080/param/get1
http://localhost:80/api 替换为了 http://localhost:8080

总之:location设置的路径,取决于proxy_pass设置的值。
proxy_pass设置的值只是ip/域名+port时 ,用proxy_pass 只替换请求url的 ip/域名+port
proxy_pass设置的值为ip/域名+port+路径时 ,用proxy_pass 替换请求url的ip/域名+port+命中路径

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值