Cannot start multiple NodeJs servers on the same subdomain

I am trying to run multiple NodeJs servers for (official) Kik Chatbots with different webcams from the same subdomain on my web server.

However, I cannot do this. For one bot, this works fine. This is my setup for production server only one :

Suppose all sites 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 configuration 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. It works great! But here is the problem :

If I try to start several NodeJs servers by creating directories in the public_html folder, say /bot1 and /bot2 and adapt my nginx configuration as follows:

  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 up a second server to listen on port 8090 instead of 8080 and, of course, setting the base URL at https://bots.mydomain.com/bot1 or https://bots.mydomain.com/bot2 , more nothing works. And I mean that web hosts do not send any data to the NodeJs server. They are, however, running! I know this because if I go to (for example) https://bots.mydomain.com and the bot is offline , I obviously get a 502 Bad Gateway error, but if the bot is online , I get a timeout (which means the server is really listening).

Am I missing something or is Nginx just not allowing multiple web hosts or proxy_passes for directories?

A workaround would be to make a subdomain for each bot that will work (I tried). But I would like to use auxiliary directories, not subdomains for bots.

EDIT :

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

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

on port 8080 and install baseurl in bot1 script on bots.mydomain.com/bot1 , Bot-1 works.

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

Does this mean that there is a problem listening to the Kik-API?

EDIT 2 :

I checked Nginx Log now, and it seems that Kik Wrapper is trying to listen to a directory that does not exist. I did the following: Launch the bot on port 8080 and report it. This is the output of the log:

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 it. As a test, I created the incoming directory 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?

+5
source share
2 answers

I think your problem is with a trailing slash in proxy_pass that removes the /bot1 and /bot2 prefixes that once passed (replacing both with a simple / ), so each bot in your nodejs has an inappropriate baseUrl parameter as a result (as you mentioned that you have modified these settings accordingly 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 
+1
source

This probably doesn't happen because your target servers get a path that includes the /bot1 and /bot2 that they cannot expect.

Perhaps try:

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

Source: https://habr.com/ru/post/1266538/


All Articles