无法在一个子域上运行多个NodeJs服务器

我试图运行多个NodeJs服务器(官方)Kik Chatbots与我的networking服务器上的一个子域不同的webhooks。

但是,我无法做到这一点。 对于一个机器人它工作得很好。 这是我的设置只有一个工作的NodeJs服务器:

让我们假设所有的webhook都位于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); 

所以这个Nodejs服务器基本上是监听端口8080 。 因此,我的网站https://bots.mydomain.com nginxconfiguration如下所示:

 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 } 

到现在为止还挺好。 这工作非常好! 但问题在于:

如果我尝试运行多个NodeJs服务器,通过在public_html文件夹中创build目录,可以说/bot1/bot2并修改我的nginxconfiguration:

  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 } 

最后将第二台服务器设置为在端口8090而不是在8080上进行监听,当然也可以将基本URL设置为https://bots.mydomain.com/bot1https://bots.mydomain.com/bot2 ,没有什么可行的了。 我的意思是,webhook不会将任何数据传递给NodeJs服务器。 他们正在运行! 我知道这一点,因为如果我导航到(例如) https://bots.mydomain.com而机器人处于脱机状态 ,我显然收到错误502 Bad Gateway但如果机器人在线,我得到一个超时(这意味着服务器确实在听)。

我错过了什么或Nginx的只是不允许多个webhooks或proxy_passes目录?

解决方法是为每个机器人创build一个子域,这将工作(我试过)。 但我想使用子目录,而不是子站点的机器人。

编辑

我注意到一个奇怪的行为:如果我设置一个proxy_pass为/

比如: location / { proxy_pass http://localhost:8080; } location / { proxy_pass http://localhost:8080; }

到端口8080,并将bot1脚本中的baseurl设置为bots.mydomain.com/bot1,Bot-1工作。

但我显然还是不能让其他机器人工作,因为我使用根(/)。

这是否意味着Kik-API的倾听方式是一个问题?

编辑2

我现在检查了Nginx日志,好像Kik Wrapper试图在一个不存在的目录上监听。 我做了以下事情:在端口8080上启动机器人并发送消息。 这是日志输出:

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" 

但我仍然不知道如何解决这个问题。 作为一个testing,我创build了在public_html incoming的目录。 这在日志中返回了以下内容:

 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" 

有没有人有如何解决它的想法?

我认为你的问题在于proxy_pass的尾部斜线,它会在传递到上游时删除/bot1/bot2前缀(仅替换为/ ),因此, nodejs代码中的每个bot都具有不匹配的baseUrl设置正如你所提到的那样,你确实改变了这些设置以匹配外部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 

它可能不会/bot1 ,因为你的目标服务器得到了包含/bot1/bot2前缀的路径,这是他们可能不希望的。

也许尝试:

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