套接字挂起错误 Http-Proxy NodeJS

13

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

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);
2个回答

12
您需要处理每个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


2
在设置代理时,向 options 中添加 changeOrigin: true,它将更改主机头的来源。当代理是“基于名称的” /不是 IP/ 时,这是必需的。
简短摘要: 实现基于名称的虚拟主机所需的技术前提是,需要一个具备 HTTP/1.1 支持(如今很普遍)并包含目标主机名请求的网络浏览器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接