Search code examples
node.jsproxyopenshiftappfogproxy-server

How do you get this node.js proxy server to work on a host like openshift or appfog?


This node.js proxy server works perfectly on my Ubuntu server (when I change the host to the server IP and the port to 8080). But this server fails on Openshift.
server.js:

var
    url = require('url'),
    http = require('http');

console.log ('------------------------------------------   ' );
var host = process.env.OPENSHIFT_NODEJS_IP || "localhost";
var port = process.env.OPENSHIFT_NODEJS_PORT || 80;
console.log(port + '   '+ host);
var d = require('domain').create();
d.on('error', function(err){
    // handle the error safely
    console.log('d.on error: '+err.message);
});

// catch the uncaught errors in this asynchronous or synchronous code block
d.run(function(){
    // the asynchronous or synchronous code that we want to catch thrown errors on
    http.createServer(function ( request, response ) {

        console.log('request ' + request.url);

        //-----------------------------------

            request.pause();
            var options = url.parse(request.url);
            options.headers = request.headers;
            options.method = request.method;
            options.agent = false;

            var connector = http.request(options, function(serverResponse) {
                serverResponse.pause();
                response.writeHeader(serverResponse.statusCode, serverResponse.headers);
                serverResponse.pipe(response);
                serverResponse.resume();
            });

            request.pipe(connector);
            request.resume();


        //-----------------------

    }).listen(port, host);
});

This server logs 8080 127.13.56.111 as port and host. Getip.com gives 54.166.197.111 as the application url's (examplesite.rhcloud.com) ip address. When I ping the application url I get this ip: 10.30.224.111 As my LAN proxy settings in the browser I used all kinds of combinations like:

54.166.197.111 80

127.13.56.111 8080

10.30.224.111 80

None of them work. My http requests form the browser, after changing the LAN proxy settings, don't get to my server. It doesn't log these requests. How can I reach the server? What do I have to change in the script to make this proxy server work on openshift? (Or maybe you know an answer for appfog.)

Here my package.json:

{
  "name": "OpenShift-Sample-App",
  "version": "1.0.0",
  "description": "OpenShift Sample Application",
  "keywords": [
    "OpenShift",
    "Node.js",
    "application",
    "openshift"
  ],
  "author": {
    "name": "OpenShift",
    "email": "[email protected]",
    "url": "http://www.openshift.com/"
  },
  "homepage": "http://www.openshift.com/",
  "repository": {
    "type": "git",
    "url": "https://github.com/openshift/origin-server"
  },

  "engines": {
    "node": ">= 0.6.0",
    "npm": ">= 1.0.0"
  },

  "dependencies": {
    "domain": "0.0.0"
  },
  "devDependencies": {},
  "bundleDependencies": [],

  "private": true,
  "main": "server.js"
}

Solution

  • With OpenShift Online, there is a reverse proxy between your gear and the internet. Here is a diagram that might be of use https://help.openshift.com/hc/en-us/articles/203263674-What-external-ports-are-available-on-OpenShift-. So while your OPENSHIFT_NODEJS_IP/PORT environment variables may report those IP's their not going to be directly accessible to the outside.