如何從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;' –