非标准的http header

  real ip

  =====

  在实际的应用中,通常需要取得客户端的ip进行处理,但是假如应用和客户端之间有台nginx服务器,那么request.getRemoteAddr()获取的通常是nginx的ip地址。那么有什么解决办法呢?

  >The [X-Forwarded-For] [3] (XFF) HTTP header field is a [de facto standard] [2] for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer.

  nginx中有一个专门的模块[ngx_http_realip_module] [1]就是用来解决这个问题的。

  - X-real-ip

  应用场景:

http://www.kingmoke.cn
http://www.lvsenvshen.cn
http://www.shmcauto.com.cn
http://www.tsjqwj.com.cn
http://www.ptsbw.com.cn
http://www.sonqi.net.cn
http://www.tzcxdd.com
http://www.gongneihzp.com
http://www.aivino.com.cn
http://www.xycarpet.cn

http://www.huihui.cn/share/32960004
http://www.huihui.cn/share/32958280
http://www.huihui.cn/share/32957358
http://www.huihui.cn/share/32956760
http://www.huihui.cn/share/32955260
http://www.huihui.cn/share/32954604
http://www.huihui.cn/share/32954146
http://www.huihui.cn/share/32953522
http://www.huihui.cn/share/32952548
http://www.huihui.cn/share/32950878
http://www.huihui.cn/share/32949902
http://www.huihui.cn/share/32949532
http://www.huihui.cn/share/32948930
http://www.huihui.cn/share/32252372
http://www.huihui.cn/share/32251552
http://www.huihui.cn/share/32251048
http://www.huihui.cn/share/32250130
http://www.huihui.cn/share/32249588
http://www.huihui.cn/share/29997150
http://www.huihui.cn/share/29996422
http://www.huihui.cn/share/29995536
http://www.huihui.cn/share/29994856
http://www.huihui.cn/share/29993976

  在客户端和应用服务之间有一台nginx,即:client->nginx->web app。那么我们只需要在配置nginx时加上 proxy_set_header X-real-ip $remote_addr; 那么web app通过获取请求头“X-real-ip”便可获得客户端ip。

  - X-Forwarded-For

  应用场景:

  在客户端与应用服务之间有两台nginx,即:client->nginx a=>nginx b=>web app,web app希望获取client和nginx a的ip地址,则应该在nginx a和nginx b上同时配置proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 这样在web app应该会获取到:X-Forwarded-For client-ip,nginx-a-ip;而nginx b获取到的X-Forwarded-For应该为client-ip。

  ##x-sendfile##

  听到[x-sendfile] [5]自然会想到sendfile,那么两者有何关联呢?都是减少copy次数。

  应用场景:

  在客户端和应用服务之间有一台nginx,即:client->nginx->web app。用户希望获取web app上的一个文件,

  1.那么正常情况下文件的请求和返回顺序:client->nginx->web app->nginx->client。这样做有什么坏处呢?暂且不说web app有可能不善于处理静态文件,内存消耗,超时,断线都会造成很大的消耗。

  2.那我们为何不考虑把文件放在更善于处理静态文件的nginx上呢。

  Good idea. 那我们在遇到请求文件时,直接nginx返回就行了,无需经过web app,即文件的请求和返回顺序为:client->nginx->client。

  3.但是如果我们需要权限判断怎么办呢?这时候x-sendfile就应该入场了。需要在nginx中添加如下配置。

  {% highlight bash %}

  location /protected/ {

  internal;

  root /some/path;

  }

  {% endhighlight %}

  这时候处理顺序为:请求到达web app=>web app进行权限验证通过后,会在响应头中添加X-Accel-Redirect:path to file=>nginx 根据path to file 找到文件返回给客户端。

  参考链接:

  [使用 Nginx 的 X-Sendfile 机制提升 PHP 文件下载性能] [4]

  [Nginx Wiki X-accel] [6]

  [1]: http://nginx.org/en/docs/http/ngx_http_realip_module.html

  [2]: http://en.wikipedia.org/wiki/De_facto_standard

  [3]: http://en.wikipedia.org/wiki/X-Forwarded-For

  [4]: http://www.lovelucy.info/x-sendfile-in-nginx.html

  [5]: http://wiki.nginx.org/XSendfile

  [6]: http://wiki.nginx.org/X-accel

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值