ITPub博客

首页 > Linux操作系统 > Linux操作系统 > nginx proxy_pass 和 proxy_redirect

nginx proxy_pass 和 proxy_redirect

Linux操作系统 作者:forest7707 时间:2017-05-18 22:00:55 0 删除 编辑
proxy_pass:充当代理服务器,转发请求
proxy_redirect:修改301或者302转发过程中的Location。默认值为proxy_redirect default。

例:
location / {
              proxy_pass http://192.168.8.46:8080/; #/结尾
              #proxy_redirect default #此为默认值,加不加都一样。
         }
这样代理到其它机器的8080端口,访问的时候都没问题,
Location: http://192.168.8.46/haha4/,浏览器的url地址栏也是http://192.168.8.46/haha4/

若:
location / {
              proxy_pass http://192.168.8.46:8080;#去掉/
              proxy_redirect off #修改默认值default为off
         }
如果去掉最后的/以后,curl -I http://192.168.8.46/haha4 访问
Location: http://192.168.8.46:8080/haha4/
浏览器访问显示的地址栏为http://192.168.8.46:8080/haha4/,(如果还是之前的,需要先删缓存)
可以看到,真实的Location地址全部暴露出来的,这个时候就需要使用proxy_redirect修改这个Location
配置如下:
location / {
              proxy_pass http://192.168.8.46:8080;
             proxy_redirect http://192.168.8.46:8080/haha4/ http://192.168.8.46/haha4/;
         }
这样,就能修改Location的地址,Location: http://192.168.8.46/haha4/,在浏览器里也是如此,就不会暴露端口号等信息,
当然,你还可以把Location弄到其它网站上去,例如
proxy_redirect http://192.168.8.46:8080/haha4/ http://www.douban.com/;
然后浏览器就跳过去了。


总结:
一切幕后黑手就是
 proxy_pass http://192.168.8.46:8080; 不加/结尾,只要把/加上,proxy_redirect 用默认值就OK了。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21220384/viewspace-2139314/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-05-17

  • 博文量
    27
  • 访问量
    20434