nginx 反向代理 某个目录下 带特定后缀名的文件

因为现在前端开发有时开发环境和生产环境 url路径不同,所以前端基本会采用nginx反向代理接口

举个例子


想把http://localhost/project/test.action 反向代理到 http://yourserver.com/test.action


[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. location ~^/project/.*.action$ {  
  2.         rewrite ^/project/(.*).action /$1.action break;  
  3.         proxy_pass  http://yourserver.com;  
  4.           
  5. }  


配置文件如上


有个坑需要注意一下

在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

location ^~ /static_js/ 

proxy_cache js_cache; 

proxy_set_header Host js.test.com; 

proxy_pass http://js.test.com/

}

如上面的配置,如果请求的url是http://servername/static_js/test.html

会被代理成http://js.test.com/test.html

而如果这么配置

location ^~ /static_js/ 

proxy_cache js_cache; 

proxy_set_header Host js.test.com; 

proxy_pass http://js.test.com

}

则会被代理到http://js.test.com/static_js/test.htm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值