Socket Hangup error Http-Proxy NodeJS
Asked Answered
R

2

13

I am getting following error in my server console when user click signout button.

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)

Here is my proxy_server:

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);
Ramakrishna answered 13/8, 2014 at 6:8 Comment(0)
C
12

You need to handle errors on each of your httpProxy objects. For example:

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

This thread was useful to me: https://github.com/nodejitsu/node-http-proxy/issues/527

Consumer answered 27/11, 2014 at 19:2 Comment(0)
S
2

Add changeOrigin: true, to the options when you set up the proxy. It changes the origin of the host header. When the proxy is "name based" /not an ip/, its a requirement. Short summary: Technical prerequisite needed for name-based virtual hosts is a web browser with HTTP/1.1 support (commonplace today) to include the target hostname in the request

Stranglehold answered 22/4, 2021 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.