nginx 如何获取真实的 IP - V2EX
testVmap

nginx 如何获取真实的 IP

  •  
  •   testVmap Sep 18, 2018 4926 views
    This topic created in 2794 days ago, the information mentioned may be changed or developed.

    客户端请求 A 服务器,A 服务器转发到 B B 的 nginx 日志是 A 的内网 IP,如何获得客户端的请求 IP

    172.16.16.16 - - [09/Jun/2018:22:34:33 +0800] "GET /sogood/info.php HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"

    Supplement 1    Sep 18, 2018
    获取到了真实 IP 加入黑名单似乎有问题
    在 B 服务器添加了黑名单
    使用 blacklist.conf,然后 nginx.conf 里面 include blacklist.conf.

    问题:
    直接访问 B 服务器,黑名单生效
    通过访问 A 服务器转发到 B 服务器的请求似乎绕过了黑名单,能访问服务器,黑名单无法生效。
    该怎样限制 IP
    10 replies   &nsp;2018-09-18 15:51:35 +08:00
    whatever93
        1
    whatever93  
       Sep 18, 2018 via Android
    x-forward-for
    throns
        2
    throns  
       Sep 18, 2018
    可以看看这个配置:![Snipaste_2018-09-18_13-58-25.png]( https://i.loli.net/2018/09/18/5ba09414b3d96.png)
    testVmap
        3
    testVmap  
    OP
       Sep 18, 2018
    A 服务器转发配置
    location ^~ /sogood {
    proxy_pass http://172.16.0.7:81;
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header Cookie $http_cookie;
    proxy_set_header Referer $http_refere;
    proxy_set_header X-Forwarded-For
    $proxy_add_x_forwarded_for;
    #proxy_set_header X-Forwarded-For $remote_addr;
    #proxy_set_header REMOTE-HOST $remote_addr;

    }


    B 服务器转发配置
    location ~ \.php$ {
    proxy_pass http://127.0.0.1;
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header Cookie $http_cookie;
    proxy_set_header Referer $http_refere;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }



    加了感觉还是没用
    lshero
        4
    lshero  
       Sep 18, 2018
    取 header 里的 X-Real-IP 啊
    testVmap
        5
    testVmap  
    OP
       Sep 18, 2018
    @lshero 是要怎么配置,要修改 log_format 吗
    testVmap
        6
    testVmap  
    OP
       Sep 18, 2018
    A 服务器的日志能正常获取到真实 IP
    throns
        7
    throns  
       Sep 18, 2018 via Android   1
    lshero
        8
    lshero  
       Sep 18, 2018   1
    @testVmap 如果是从日志获取的话。log_format 可以参考一下这个 http://blog.51cto.com/nicking/1431056。如果程序要取的话把所有的 header 里添加的 X-Forwarded-For 或者 X-Real-IP 都成
    jtsai
        9
    jtsai  
       Sep 18, 2018   1
    A 服务把客户端的请求头转到 B 服务器

    location / {
    # HTTP header
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass (B 服务器地址)

    }
    testVmap
        10
    testVmap  
    OP
       Sep 18, 2018
    感谢大家,最后配置如下
    服务器 A 的转发配置
    location ^~ /namesg/ {
    proxy_pass http://172.16.16.11;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    }

    服务器 B 的 nginx.conf log 配置

    log_format my_format '$http_x_real_ip -'
    '$remote_addr- [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log logs/access.log my_format;


    主要是 nginx 日志取消注释后还不能生效,需要改下日志名字
    About     Help     Advertise     Blog     API     FAQ     Solana     4200 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 05:12 PVG 13:12 LAX 22:12 JFK 01:12
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86