【问题标题】:Socket Hangup error Http-Proxy NodeJS套接字挂断错误 Http-Proxy NodeJS
【发布时间】:2014-10-06 09:33:32
【问题描述】:

当用户单击退出按钮时,我的服务器控制台中出现以下错误。

Error: socket hang up
    at createHangUpError (http.js:1472:15)
    at Socket.socketCloseListener (http.js:1522:23)
    at Socket.EventEmitter.emit (events.js:95:17)
    at TCP.close (net.js:466:12)

这是我的代理服务器:

var fs=require('fs');
var options = {
    key: fs.readFileSync('key/xxxxxxxxx.pem'),
    cert: fs.readFileSync('key/xxxxx.pem'),
    ca: fs.readFileSync('key/gd_bundle-g2-g1.crt')
};

var express=require('express'),
    https=require('https'),
    httpProxy = require('http-proxy'),
    http=require('http'),
    app=express(),
    app1=express(),
    server=https.createServer(options,app),
    serverhttp=http.createServer(app1);

var proxy = httpProxy.createProxy({ target: 'http://localhost:9898',secureOptions:'constants.SSL_OP_NO_TLSv1_2'});
var proxySig = httpProxy.createProxy({target:'http://localhost:8881',secureOptions:'constants.SSL_OP_NO_TLSv1_2'});
var callSig = httpProxy.createProxy({target:'http://localhost:6666',secureOptions:'constants.SSL_OP_NO_TLSv1_2'});
var proxyCdn = httpProxy.createProxy({target:'http://localhost:3030',secureOptions:'constants.SSL_OP_NO_TLSv1_2'});
// var proxyhttps= new httpProxy.createProxy({ target: 'https://localhost:443',secure:false});
var errorhandler = require('errorhandler');

app.all('*', function(req,res){
    if(req.hostname=='xxxxxxxx.in')
    {
        proxy.web(req, res); 
    }
    else if(req.hostname=='xxxx.in')
    {
        proxyCdn.web(req, res);
    }
    else if(req.hostname=='xxxxxx.in')
    {
        proxySig.web(req, res); 
    }
    else if(req.hostname=='xxxxx.in')
    {
        callSig.web(req, res); 
    }
});

app1.all('*', function(req,res){
    res.redirect('https://'+req.hostname);;
});

serverhttp.listen(80);
server.listen(443);

【问题讨论】:

    标签: node.js express socket.io http-proxy


    【解决方案1】:

    在设置代理时将changeOrigin: true 添加到options。它改变了主机头的来源。当代理是“基于名称的”/不是 ip/ 时,这是一个要求。 简短的摘要: 基于名称的虚拟主机所需的技术先决条件是支持 HTTP/1.1 的 Web 浏览器(现在很常见),以便在请求中包含目标主机名

    【讨论】:

      【解决方案2】:

      您需要处理每个 httpProxy 对象上的错误。例如:

      proxy.on('error', function (error, req, res) {
          var json;
          console.log('proxy error', error);
          if (!res.headersSent) {
              res.writeHead(500, { 'content-type': 'application/json' });
          }
      
          json = { error: 'proxy_error', reason: error.message };
          res.end(JSON.stringify(json));
      });
      

      这个帖子对我很有用:https://github.com/nodejitsu/node-http-proxy/issues/527

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-17
        • 2013-10-05
        • 2013-11-16
        • 2014-12-25
        • 2015-09-13
        • 2015-10-30
        • 2019-08-02
        • 2014-04-28
        相关资源
        最近更新 更多