Cannot run multiple NodeJs server on one subdomain

半世苍凉 提交于 2019-12-23 09:49:36

问题


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.

EDIT:

I noticed a strange behavior: If I set a proxy_pass for /

like: location / { proxy_pass http://localhost:8080; }

to port 8080 and set the baseurl in the bot1 script to bots.mydomain.com/bot1, Bot-1 works.

But I obviously still can't get other bots to work aswell because I'm using the root (/).

Does that mean it's a problem with Kik-API's way of listening?

EDIT 2:

I checked the Nginx Log now and it seems like the Kik Wrapper tries to listen on a directory which doesn't exists. I did the following: Start the bot on port 8080 & message it. This is the log output:

https://pastebin.com/7G6TViHM

2017/04/13 09:07:05 [error] 15614#15614: *1 open() "/var/www/bots.mydomain.com/public_html/incoming" failed (2: No such file or directory), client: 107.XXX.XXX.XXX, server: bots.mydomain.com, request: "POST /incoming HTTP/1.1", host: "bots.mydomain.com"
2017/04/13 09:07:13 [error] 15614#15614: *1 open() "/var/www/bots.mydomain.com/public_html/incoming" failed (2: No such file or directory), client: 107.XXX.XXX.XXX, server: bots.mydomain.com, request: "POST /incoming HTTP/1.1", host: "bots.mydomain.com"

But I still don't know how to fix this. As a test I created the directory incoming in public_html. This returned the following in the log:

2017/04/13 09:32:41 [error] 15614#15614: *10 directory index of "/var/www/bots.mydomain.com/public_html/incoming/" is forbidden, client: 107.XXX.XXX.XXX, server: bots.mydomain.com, request: "GET /incoming/ HTTP/1.1", host: "bots.mydomain.com"

Does anyone have an idea on how to fix it?


回答1:


I think your issue lies with a trailing slash in proxy_pass, which removes the /bot1 and /bot2 prefixes once passed to upstream (replacing both with mere /), so, each bot in your nodejs code has a mismatched baseUrl setting as a result (as you mention that you did change those settings appropriately to match the external URL).

-location /bot1 { proxy_pass http://localhost:8080/; } # Port 8080
-location /bot2 { proxy_pass http://localhost:8090/; } # Port 8090
+location /bot1 { proxy_pass http://localhost:8080; } # Port 8080
+location /bot2 { proxy_pass http://localhost:8090; } # Port 8090



回答2:


It probably doesn't wok because your target servers get the path that includes the /bot1 and /bot2 prefix, which they may not expect.

Maybe try:

location /bot1 {
    rewrite ^/bot1/(.*)$ /$1 break;
    proxy_pass http://localhost:8080/;

}
location /bot2 {
    rewrite ^/bot2/(.*)$ /$1 break;
    proxy_pass http://localhost:8090/;
}


来源:https://stackoverflow.com/questions/43317593/cannot-run-multiple-nodejs-server-on-one-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!