• linux下利用tcpdump抓包工具排查nginx获取客户端真实IP实例


    一、nginx后端负载服务器的API在获取客户端IP时始终只能获取nginx的代理服务器IP,排查nginx配置如下

    upstream sms-resp {
             server 192.168.5.216:8501;
             server 192.168.5.217:8501;
        }
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                #root   html;
                #index  index.html index.htm;
                proxy_pass  http://sms-resp;
                proxy_set_header host $host;
                proxy_set_header X-real-ip $remote_addr;
                proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
            }

    nginx配置项已经配置了转换真实客户端IP的参数设置,那么需要抓包来看看是否是真正的转换了客户端IP。

    二、安装tcpdump抓包工具

    在nginx和后端API服务器上分别安装tcpdump

    [root@push-5-216 ~]# yum install -y tcpdump

    三、使用tcpdump抓包

    在172.28.146.109上浏览器调用172.28.5.215的nginx代理的HTTP接口,nginx将请求分发到172.28.5.216上,这里nginx和后端API均配置双网卡(172.28.5.215182.168.5.215172.28.5.216192.168.5.216),他们之间走的192.168网段。

    在172.28.5.215的80端口上抓取从172.28.146.109过来的TCP包,同时nginx会将请求转发到192.168.5.216的8501端口上,同时也在172.28.5.216的8501端口上上抓取从192.168.5.215上过来的tcp包

    在172.28.5.215上执行

    [root@push-5-215 ~]# tcpdump -i em1 tcp port 80 and host 172.28.146.109 -c 100 -n -vvv -w /opt/nginx-215.cap
    tcpdump: listening on em1, link-type EN10MB (Ethernet), capture size 262144 bytes
    Got 0

    -i:监听哪个网卡

    tcp:监听哪个协议包(tcpudpssh)

    port:监听端口

    and host:监听指定IP地址进入的数据包(入)

    dst host :监听发给指定IP地址的数据包(出)

    -c:监听多少个数据包

    -n:显示IP地址

    -vvv:显示详细信息

    -w:将监听信息输出到文件

    在172.28.5.216上执行

    [root@push-5-216 ~]# tcpdump -i em2 port 8501 and host 192.168.5.215 -c 100 -n -vvv -w /opt/nginx-216.cap 
    tcpdump: listening on em2, link-type EN10MB (Ethernet), capture size 262144 bytes
    Got 0

    然后在172.28.146.109上调用测试接口:http://172.28.5.215/sms-report?xxxxxxx=xxxxxx&xxx=xxxxxxx, 此时,在抓包的两台服务器上均显示有数据包捕获 

    [root@push-5-215 ~]# tcpdump -i em1 tcp port 80 and host 172.28.146.109 -c 100 -n -vvv -w /opt/nginx-215.cap
    tcpdump: listening on em1, link-type EN10MB (Ethernet), capture size 262144 bytes
    Got 22
    [root@push-5-216 ~]# tcpdump -i em2 port 8501 and host 192.168.5.215 -c 100 -n -vvv -w /opt/nginx-216.cap 
    tcpdump: listening on em2, link-type EN10MB (Ethernet), capture size 262144 bytes
    Got 10

      此时按ctrl+c中断监听,sz下载nginx-215.cap和nginx-216.cap文件到本地

    四、利用wireshark工具分析数据包日志文件

    打开nginx-215.cap

     显示调用接口的数据包

    再打开nginx-216.cap

    这条数据包即为nginx转发到216的请求数据,双击打开详细信息窗口

    在data字段里可以看到转发的请求数据,在下面的详细信息中开始部分即为http请求头,可以看到里面有X-real-ip和X-forwarded-for两个字段这是在nginx配置文件里配置的参数,里面存放客户端调用真实IP,显示为172.28.146.109即为真实调用http接口的客户端IP。

  • 相关阅读:
    京东Java面试题(二)
    京东Java面试题(一)
    阿里java面试题
    Java垃圾回收机制
    MyBatis面试题
    Java IO流总结
    Spring中文文档
    Vue.js实战之组件之间的数据传递
    Vue.js实战之Vuex的入门教程
    Vue系列——在vue项目中使用jQuery及其第三方插件
  • 原文地址:https://www.cnblogs.com/sky-cheng/p/11058221.html
Copyright © 2020-2023  润新知