【问题标题】:path based proxy with http-proxy nodejs使用 http-proxy nodejs 的基于路径的代理
【发布时间】:2014-04-02 10:20:13
【问题描述】:

我想要一个具有基于路径的路由的代理。 但我的代码不起作用

var httpProxy = require('http-proxy')

var proxy = httpProxy.createProxy();

var options = {
'example.com/app1': 'http://localhost:4444',
'example.com/app2': 'http://localhost:3333'
}

require('http').createServer(function(req, res) {
proxy.web(req, res, {
    target: options[req.headers.host]
},function(error) {

});
}).listen(80);

问题如何?

感谢您的帮助

【问题讨论】:

    标签: node.js node-http-proxy


    【解决方案1】:

    您可能想尝试express-http-proxy,它可以让您在快速路由上安装代理:

    app.use('/app1/', proxy('http://localhost:4444', {
        forwardPath: function(req, res){
          return url.parse(req.url).path.replace(/\/app1/,'/');
        }
      })
    );
    

    即使您要求使用 node.js 解决方案,我也不能真正建议采用这条路线(没有双关语的意思)。 你可能会更好地使用在这个特定用例中经过更多测试的东西——比如 nginx:

    location /app1/ {
      proxy_pass http://localhost:4444;
    }
    

    【讨论】:

      【解决方案2】:

      最新版本的 http-proxy 删除了 proxytable 功能。见https://github.com/nodejitsu/node-http-proxy/blob/master/UPGRADING.md 0.8.x 版本可以进行基于路径的路由。而对于当前的 http-proxy,中间件可以为它做代理 (https://github.com/dominictarr/proxy-by-url)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-14
        • 2014-01-03
        • 1970-01-01
        • 2022-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多