Monday, 10 April 2017

Cannot run multiple NodeJs server on one subdomain

I am trying to run multiple NodeJs server for (official) Kik Chatbots with different webhooks from one Subdomain on my webserver.

However, I am not able to do that. For one bot it works just fine. This is my setup for one working NodeJs server only:

Lets assume all webhooks are located at https://bots.mydomain.com

app.js:

'use strict';
let util = require('util');
let http = require('http');
let request = require('request');
let Bot = require('@kikinteractive/kik');
let bot = new Bot({
    username: "foo",
    apiKey:   "bar",
    baseUrl:  "https://bots.mydomain.com"
});
bot.updateBotConfiguration();

// ... code ...

let server = http.createServer(bot.incoming()).listen(process.env.PORT || 8080);

So this Nodejs server is basically listening on port 8080. Therefore, my nginx config for the site https://bots.mydomain.com looks like this:

server {
    root /var/www/bots.mydomain.com/public_html;
    index index.php index.html index.htm;
    server_name bots.mydomain.com;
    location / { proxy_pass http://localhost:8080/; } # Port 8080
}

So far so good. This works perfectly fine! But here comes the issue:

If I try to run multiple NodeJs server, by making directories in the public_html folder, lets say /bot1 and /bot2 and adapt my nginx config like that:

 server {
    root /var/www/bots.mydomain.com/public_html;
    index index.php index.html index.htm;
    server_name bots.mydomain.com;
    location /bot1 { proxy_pass http://localhost:8080/; } # Port 8080
    location /bot2 { proxy_pass http://localhost:8090/; } # Port 8090
}

and finally setting the second server to listen on port 8090 instead of 8080 and of course setting the base URL to https://bots.mydomain.com/bot1 or https://bots.mydomain.com/bot2, nothing works anymore. And by that I mean the webhooks do no pass any data to the NodeJs server. They are, however running! I know this because if I navigate to (for example) https://bots.mydomain.com while the bot is offline, I obviously receive the error 502 Bad Gateway but if the bot is online I get a timeout (which means the server is indeed listening).

Am I missing something or does Nginx just not allow multiple webhooks or proxy_passes for directories?

A workaround would be to make a subdomain for each bot, which would work (I tried). But I'd like to use sub directories rather than subdomains for the bots.



via NullDev

No comments:

Post a Comment