博客
关于我
nodejs+nginx获取真实ip
阅读量:792 次
发布时间:2023-02-16

本文共 1466 字,大约阅读时间需要 4 分钟。

配置Nginx

location / {            proxy_set_header Host $http_host;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_pass http://127.0.0.1:8360/;        }

Node.js获取IP地址

var getIp = function(_http) {            var ipStr = _http.headers['X-Real-IP'] || _http.headers['x-forwarded-for'];            if (ipStr) {                var ipArray = ipStr.split(",");                if (ipArray && ipArray.length > 0) {                    return ipArray[0];                }            } else {                return _http.ip().substring(_http.ip().lastIndexOf(":") + 1);            }        };     var getNetIp = function(_http) {        var ipStr = _http.headers['X-Real-IP'] || _http.headers['x-forwarded-for'];        if (ipStr) {            var ipArray = ipStr.split(",");            if (ipArray.length > 1) {                for (var i = 0; i < ipArray.length; i++) {                    var ipNumArray = ipArray[i].split(".");                    var tmp = ipNumArray[0] + "." + ipNumArray[1];                    if (tmp == "192.168" || (ipNumArray[0] == "172" && ipNumArray[1] >= 16 && ipNumArray[1] <= 32) || tmp == "10.7") {                        continue;                    }                    return ipArray[i];                }            }            return ipArray[0];        } else {            return _http.ip().substring(_http.ip().lastIndexOf(":") + 1);        }    };

转载地址:http://ctjfk.baihongyu.com/

你可能感兴趣的文章
Netty工作笔记0063---WebSocket长连接开发2
查看>>
Netty工作笔记0070---Protobuf使用案例Codec使用
查看>>
Netty工作笔记0077---handler链调用机制实例4
查看>>
Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
查看>>
Netty工作笔记0085---TCP粘包拆包内容梳理
查看>>
Netty常用组件一
查看>>
Netty常见组件二
查看>>
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty心跳检测机制
查看>>
Netty核心模块组件
查看>>
Netty框架内的宝藏:ByteBuf
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—3.Reactor线程模型三
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—5.Pipeline和Handler二
查看>>
Netty源码—6.ByteBuf原理一
查看>>
Netty源码—6.ByteBuf原理二
查看>>