如何从http req对象获取客户端IP地址?node.js从http请求对象获取客户端IP
IE:
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('../../lib/node-http-proxy');
//
// Http Server with proxyRequest Handler and Latency
//
var proxy = new httpProxy.RoutingProxy();
http.createServer(function (req, res) {
// GET IP address here
// var ip = ??
var buffer = httpProxy.buffer(req);
setTimeout(function() {
proxy.proxyRequest(req, res, {
port: 9000,
host: 'localhost',
buffer: buffer
});
}, 200);
}).listen(8004);
代理服务器的事实标准是“X-Forwarded-For”。它支持大多数缓存代理,并且可以在nginx中配置:'proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;' –