我是中国一所学校的老师,互联网带宽非常有限。学校没有内部服务器(代理、电子邮件或文件服务器),我有兴趣在我的 MacBook Pro 上实现自己的服务器,以改善学生的学习体验。
我已经设置了一个 node
(欣喜若狂)网络服务器,用于共享我的文件和笔记。我认为下一步是代理服务器。
我想设置代理服务器以:
- 减少带宽使用
- 提高学生的速度
- 可能允许通过我的 VPN 访问网站。
例如,所有学生可能都在使用 bbc bidize http://www.bbc.co.uk/education并且同时使用同一页面,因此缓存理论上会有很大帮助
我正在使用 javascript 和 Node,所以如果这在 Node 中是可能的,那就太好了,如果不是,我愿意使用 python、java 或其他。理想情况下,解决方案应该是开箱即用的,但随着我的技能发展,我有改进和调整它的空间。
我希望学生连接到我的计算机上的 myIP:port 并像在网站上一样浏览。
或者(但不太优选)我可以让他们将我的计算机添加为他们的互联网设置中的代理,并以这种方式运行服务器(任何建议)
感谢您的帮助,我的学生将不胜感激。
保罗
以下是我尝试过的一些方法:
我尝试过node-http-proxy
httpProxy.createProxyServer({target:'http://www.bbc.co.uk/'}).listen(9000);
但它不允许我浏览(只是将我转储到未找到页面),并且我不确定它是否会有效缓存。
Simple Proxy Server with NodeJs
var app = express();
app.use('/', function(req, res) {
var url = 'https://xkcd.com/' + req.url;
var options = {
url: url,
rejectUnauthorized: false
}
req.pipe(request(url)).pipe(res);
});
app.listen(8001, function(){console.log("listen on 8001");})
这适用于 xkcd.com,但不适用于 www.bbc.co.uk,我预计 cookie 可能是问题所在,而且由于许多链接是绝对的,我需要拦截它们并将它们替换为相对于代理的链接。
Node.js caching proxy server 这看起来很有希望,但我正在寻找一个更完整的解决方案,因为这有点超出我的能力范围。
最佳答案
您可以使用node-proxy-cache
:
var ProxyCache = require( 'node-proxy-cache' ),
proxyCache = new ProxyCache({}),
DS = {};
proxyCache.when( /google/, {
getKey: function( path, query ) { // allows you to generate keys
return 'foo:' + path;
},
headers: {
'X-Bar': 'Baz' // set custom headers when sending to proxy
},
caching: false // don't cache responses from google
});
proxyCache.when( /foo.org/, {
cacheTime: function( cacheEntry, req, proxyRes ) {
if ( cacheEntry.body.length > 10000000 ) {
return -1; // don't cache big responses
}
if ( req.url.match( /bar/) ) {
return 0; // cache bar stuff forever
}
if ( proxyRes.statusCode === 404 ) {
return -1; // don't cache 404 responses
}
return 10000; // only cache for 10 seconds
}
});
proxyCache.store({ // custom storeAdapter
get: function( key, callback ) {
callback( null, DS[key] ); // getting information
},
set: function( key, value, callback ) { // setting values to store
DS[ key ] = value; // value.body is a buffer
callback( );
}
});
proxyCache.listen( 9000 );
关于javascript - 如何在nodejs中设置网络缓存代理服务器以减少互联网使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35883562/