Nginx proxy pass路由转发简单用法




Nginx proxy pass简单用法 - 只做最好的运维 - 51CTO技术博客  http://stevenlee87.blog.51cto.com/996623/1188295


Nginx proxy pass简单用法


(把下面内容修改到nginx的配置虚拟主机的配置文件中, 然后重启nginx即可)


一,在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;
    } 
如上面的配置,如果请求的url是http://servername/static_js/test.html
则会被代理到http://js.test.com/static_js/test.htm

二,关于proxy_pass配置的uri问题

你不想nginx对你的URI请求有任何形式的修改,那么,proxy_pass的配置中就不应该有任何URI部分。

举个例子,nginx服务器IP为10.0.0.20,它的配置不含URI:
location /firstcond/ {
        proxy_pass http://10.0.0.30:90;
}
那么,
原:http://10.0.0.20/first/second/test.html
转:http://10.0.0.30:90/first/second/test.html

如果配置成含URI:
location /firstcond/ {
proxy_pass http://10.0.0.30:90/myuri;
}
那么,
原: http://10.0.0.20/first/second/test.html
转:http://10.0.0.30:90/myuri/test.html
简单地说,配置了URI之后,跳转行为可能会令你感到莫名其妙。

注意:

1, 假如是要转发到10.10.10.10:5601时, 若链接末尾不带/, 若访问test.com/aa/bb.html, 会转发成proxy_pass http://10.10.10.10:5601/aa/bb.html, 而带/, 就转发成http://10.10.10.10:5601/bb.html

2, 假如10.10.10.10:5601是有认证的, 则访问test.com, 也需要认证, 反正看成访问test.com的效果就等同于10.10.10.10:5601的效果即可



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值