简体   繁体   English

如何在Node.js中设置Web缓存代理服务器以减少Internet使用

[英]How can I set up a web cache-proxy server in nodejs to reduce internet usage

I am a teacher at a school in China with very limited internet bandwidth. 我是中国一所学校的老师,互联网带宽非常有限。 The school has no internal servers (proxy, email or file server) and I am interested in implementing my own on my macbook pro in order to improve the students' learning experience. 学校没有内部服务器(代理,电子邮件或文件服务器),我有兴趣在自己的Macbook Pro上实施自己的服务器,以改善学生的学习体验。

I have already set up a node (ecstatic) webserver that I use to share my files and notes. 我已经设置了一个node (静态)Web服务器,用于共享文件和注释。 I think the next step it a proxy server. 我认为下一步是代理服务器。

I would like to set up a proxy server to: 我想将代理服务器设置为:

  1. reduce bandwidth usage 减少带宽使用
  2. improve speed for students and 提高学生的速度并
  3. potentially allow access to sites by piggybacking on my VPN. 可能通过VPN带我的VPN来允许访问站点。

For example all the students might be using bbc bitesize http://www.bbc.co.uk/education and using the same page at the same time so caching would in theory help immensely 例如,所有学生可能都在使用bbc bitesize http://www.bbc.co.uk/education并同时使用同一页面,因此从理论上讲缓存将极大地帮助您

I am playing with javascript and node so if this is possible in node that would be great, if not I am willing to use python, java or whatever. 我正在使用javascript和node进行游戏,因此,如果这在node上是可行的,那将会很棒,如果不是,我愿意使用python,java或其他任何东西。 Ideally a solution would work out of the box but leave room for me to improve and tweak it as my skills develop. 理想情况下,一种解决方案可以立即使用,但是随着我的技能发展,我有空间去改进和调整它。

I would like to students to connect to myIP:port on my machine and browse as if they were on the website. 我想让学生连接到我机器上的myIP:port并像浏览网站一样浏览。

Alternatively (but less preferably) I could get them to add my computer as a proxy in their internet settings and run a server that way (any suggestions) 或者(但不太可取),我可以让他们在自己的Internet设置中将计算机添加为代理,然后以这种方式运行服务器(任何建议)

Thanks for any help, my students will appreciate it. 感谢您的帮助,我的学生将不胜感激。

Paul 保罗

Here are some things I have tried: 这是我尝试过的一些方法:

I have tried node-http-proxy 我已经尝试过node-http-proxy

httpProxy.createProxyServer({target:'http://www.bbc.co.uk/'}).listen(9000);

but it wasn't allowing me to browse (just dumping me on the page not found page) and I am unsure if it would cache effectively. 但是它不允许我浏览(只是将我转储到未找到的页面上),我不确定它是否可以有效缓存。

Simple Proxy Server with NodeJs 具有NodeJ的简单代理服务器

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");})

This works for xkcd.com but not for www.bbc.co.uk, I expect cookies may be the problem, also as many of the links are absolute I would need to intercept them and replace them with links relative to the proxy. 这适用于xkcd.com,但不适用于www.bbc.co.uk,我希望Cookie可能是问题所在,而且由于许多链接都是绝对的,因此我需要拦截它们并将其替换为相对于代理的链接。

Node.js caching proxy server This looks promising but I am looking for a more finished solution as this is a bit over my head. Node.js缓存代理服务器这看起来很有希望,但是我正在寻找一个更完善的解决方案,因为这有点麻烦。

You can use node-proxy-cache : 您可以使用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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Node.js中为多个目标设置反向代理? - How to set up a reverse proxy in nodejs for multiple targets? 如何在Web服务器上设置gzip压缩? - How do I set up gzip compression on a web server? 如何使Node.js express.js(代理)服务器仅可用于特定域? - How can I make a Nodejs express.js (proxy) server available only for a specific domain? 如何减少 Web Worker HTML 5 的 CPU 使用率 - How to reduce CPU usage of web worker HTML 5 如何设置nodejs服务器,通过检查URL为客户端提供正确的文件? - How do I set up a nodejs server that gives the client the correct file by checking the url? 如何在Mac上设置反向代理? - How do I set up a reverse proxy on a Mac? getImageData - Web 工作者 - 如何减少垃圾收集? - getImageData - Web workers - How can I reduce garbage collection? 设置代理服务器以创建反应应用程序 - Set up proxy server for create react app 在前端按下按钮后,如何在 web 服务器(nodeJS)中运行 python 脚本? - How can I run a python script in a web server (nodeJS) after pressing a button on frontend? 如何使用Web Audio API降低麦克风输入的噪音? - How can I reduce the noise of a microphone input with the Web Audio API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM