IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nginx(tengine)无法获取自定义header头ns_clientip

    C1G发表于 2017-03-08 07:34:59
    love 0

    如果你的web服务器前端有代理服务器或CDN时日志中的$remote_addr可能就不是客户端的真实ip了。
    通常可以安装realip模块来解决。
    代码也很简单

    set_real_ip_from 192.168.1.0/24; 指定接收来自哪个前端发送的 IP head 可以是单个IP或者IP段
    set_real_ip_from 192.168.2.1;
    real_ip_header X-Real-IP; IP head 的对应参数。

    注意:基本上网上教程都是使用“X-Real-IP”做head头,今天遇到了使用”ns_clientip”做header头的,怎么也获取不到。

    先用”$http_ns_clientip” “$sent_http_clientip”在日志中尝了下无果。

    log_format combined '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" "$http_ns_clientip" "$sent_http_clientip"';

    一度怀疑前端没有带上header头,用tcp_dump确认了下确实带了。

    tcpdump tcp port 80 -n -X -s 0

    网上找了一圈,原来是对header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉。
    解决方法是在配置中http部分 增加underscores_in_headers on;
    这下log和realip都可正常取值了。

    参考:
    http://nginx.org/en/docs/http/ngx_http_log_module.html
    http://holy2010.blog.51cto.com/blog/1086044/1840481



沪ICP备19023445号-2号
友情链接