【问题标题】:How to use node-http-proxy for HTTP to HTTPS routing?如何使用 node-http-proxy 进行 HTTP 到 HTTPS 路由?
【发布时间】:2013-03-25 22:12:59
【问题描述】:

这是我正在使用的模块版本:

$ npm list -g | grep proxy
├─┬ http-proxy@0.10.0

一个网络服务调用我的机器,我的任务是根据请求正文的内容将请求代理到不同的 url 和主机:

var http      = require('http'),
    httpProxy = require('http-proxy')
    form2json = require('form2json');

httpProxy.createServer(function (req, res, proxy) {
  // my custom logic
  var fullBody = '';
  req.on('data', function(chunk) {
      // append the current chunk of data to the fullBody variable
      fullBody += chunk.toString();
  });
  req.on('end', function() {
      var jsonBody = form2json.decode(fullBody);
      var payload = JSON.parse(jsonBody.payload);
      req.url = '/my_couch_db/_design/ddoc_name/_update/my_handler?id="' + payload.id + '"';

      // standard proxy stuff
      proxy.proxyRequest(req, res, {
        changeOrigin: true,
        host: 'my.cloudant.com',
        port: 443,
        https: true
      });
  });
}).listen(8080);

但我不断遇到错误,例如:An error has occurred: {"code":"ECONNRESET"}

有人知道这里需要修复什么吗?

【问题讨论】:

    标签: node.js http-proxy node-http-proxy


    【解决方案1】:

    这对我有用:

    var httpProxy = require('http-proxy');
    
    var options = {
      changeOrigin: true,
      target: {
          https: true
      }
    }
    
    httpProxy.createServer(443, 'www.google.com', options).listen(8001);
    

    【讨论】:

    • 谢谢 Aaron,这是我很长时间以来收到的关于这个主题的第一个有用的答案,而且确实有效!
    【解决方案2】:

    将来自端口3000 的所有请求转发到https://google.com

    const https = require('https')
    const httpProxy = require('http-proxy')
    
    httpProxy.createProxyServer({
      target: 'https://google.com',
      agent: https.globalAgent,
      headers: {
        host: 'google.com'
      }
    }).listen(3000)
    

    https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js 启发的示例。

    【讨论】:

      【解决方案3】:

      经过反复试验,这对我有用:

      var fs = require('fs')
      var httpProxy = require('http-proxy');
      var https = require('https');
      
      var KEY  = 'newfile.key.pem';
      var CERT = 'newfile.crt.pem';
      
      httpProxy.createServer({
        changeOrigin: true,
        target: 'https://example.com',
        agent: new https.Agent({
          port: 443,
          key: fs.readFileSync(KEY),
          cert: fs.readFileSync(CERT)
        })
      }).listen(8080);
      

      【讨论】:

        猜你喜欢
        • 2011-12-03
        • 1970-01-01
        • 2013-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多