【发布时间】:2014-03-01 09:39:37
【问题描述】:
我正在使用以下代码在 node.js 实现反向代理。它工作正常,但问题是当我尝试访问服务器 127.0.0.1:9008/" 它很容易获得。我希望它只能通过代理服务器访问。请帮忙..
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with latency
//
var proxy = httpProxy.createProxyServer();
//
// Create your server that make an operation that take a while
// and then proxy de request
//
http.createServer(function (req, res) {
// This simulate an operation that take 500ms in execute
setTimeout(function () {
proxy.web(req, res, {
target: 'http://127.0.0.1:9008'
});
}, 500);
}).listen(8008);
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify (req.headers, true, 2));
res.end();
}).listen(9008);
【问题讨论】:
标签: node.js load-balancing reverse-proxy http-proxy