简体   繁体   中英

Node.js proxy with https

I want to setup a simple proxy which hides my ugly long host address to a nice TLD format, something like https:// www.qwertyuiop.a-verylong-texthere.co.uk -> http:// awesome.me. Unfortunately my host, does not support binding a TLD to my server.

Using RedHat's OpenShift, I have tried out the corresponding example of nodejitsu's excellent node-http-proxy library, but what it does is to redirect me to the ugly long host address instead of keeping the nice http:// awesome.me url. Can anyone please tell me what am I doing wrong or what am I missing? Any suggestions to solve the problem?

Here is my code snipped with google for testing as the target https server:

    var ipaddress = process.env.OPENSHIFT_NODEJS_IP;
    var port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

    httpProxy.createServer({
        target: 'https://google.com',
        agent  : https.globalAgent,
        headers: {
            host: 'google.com'
        }
    }).listen(port, ipaddress);

The solution is:

use the default SampleApp and create a proxy object within self:

self.proxy = httpProxy.createServer({ 
    ssl: { key: fs.readFileSync('ssl.key'), cert: fs.readFileSync('ssl.crt')},
    target: 'https://awesome.me',
    secure: true
});

then replace the routes by the following

self.routes['/*'] = function(req, res) {
    self.proxy.web(req, res);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM