我正尝试运行多个NodeJs服务器,用于从我的网络服务器上的一个子域中访问不同webhook的(官方)Kik Chatbots。无法在一个子域上运行多个NodeJs服务器
但是,我无法做到这一点。对于一个机器人它工作得很好。这是我的设置为一个工作只的NodeJS服务器:
让我们假设所有的网络挂接位于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
nginx的配置是这样的:
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
文件夹制作目录,可以说/bot1
和/bot2
和适应我的nginx的配置这样的:
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/bot1
或https://bots.mydomain.com/bot2
,但没有任何工作了。我的意思是,webhook不会将任何数据传递给NodeJs服务器。他们正在运行!我知道这一点,因为如果我导航到(例如)https://bots.mydomain.com
,而机器人是离线,我显然收到错误502 Bad Gateway
,但如果机器人是在线我得到一个超时(这意味着服务器确实在侦听)。
我错过了一些东西吗?Nginx是不是允许多个webhooks或proxy_passes目录?
解决方法是为每个机器人创建一个子域,这将工作(我试过)。但我想使用子目录而不是子域来进行漫游。
编辑:
我注意到一个奇怪的现象:如果我设置一个proxy_pass为/
,如:location/{ proxy_pass http://localhost:8080; }
8080端口,并在bot1脚本的BaseURL设置为bots.mydomain.com/bot1
, Bot-1的作品。
但我显然仍然无法让其他机器人工作,因为我使用根(/)。
这是否意味着这是Kik-API的听法问题?
编辑2:
我现在检查了Nginx的日志,它似乎像Kik的包装试图听上不存在的目录。我做了以下操作:启动机器人端口8080
&消息它。这是日志输出:
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"
但我仍然不知道如何解决这个问题。作为测试,我在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"
有没有人有关于如何解决它的想法?
如何使用'location/bot1 /'和末尾的'/'(只是作为一个实验) –
你可以分享你的nginx日志吗? –
@NullDev你可以尝试改变: 'baseUrl:“https://bots.mydomain.com”' 在每个机器人上: 'baseUrl:“https://bots.mydomain.com/bot1”' 'baseUrl:“https://bots.mydomain.com/bot2”' 并发布nginx日志。 –