抛弃Nginx使用nodejs做反向代理服务器

2020-06-17 05:33:21易采站长站整理

//
var server = require('http').createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
var host = req.url;
host = url.parse(host); host = host.host;

console.log("host:" + req.headers.host);
console.log("client ip:" + (req.headers['x-forwarded-for'] || req.connection.remoteAddress));

proxy.web(req, res, { target: 'http://localhost:8080' });
});

console.log("listening on port 80")
server.listen(80);

若说使用代理服务器的代价,可能就是会比不用消耗多的资源,消耗多的 CPU 运算罢了。

使用问题:不能指定文件夹 proxy.web(req, res, { target: ‘http://jb51.net:81/foo/’ });