如何让 rasa 网络聊天与 nginx 和 https 一起工作?
How do I make rasa webchat work with nginx and https?
我有一个使用 rasa 实现的聊天机器人,它正在使用 rasa webchat。当我在本地使用 ngrok 测试它时它工作正常,当我在服务器上配置 nginx(端口 80)时它也工作。但是,一旦我更改了 nginx 配置文件以支持 https,网络聊天只显示 "Connecting..." 消息并且没有响应。我在浏览器控制台中查找错误,但有 none。这是我的索引文件,它负责网络聊天界面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fe</title>
</head>
<body>
<div id="webchat">
<script src="https://storage.googleapis.com/mrbot-cdn/webchat-0.5.8.js"></script>
<script>
WebChat.default.init({
selector: "#webchat",
initPayload: "hi",
interval: 1000,
socketUrl: "https://example.com/rasa",
socketPath: "/socket.io/",
title: "speak with bot",
inputTextFieldHint: "say something...",
connectingText: "Connecting...",
connectOn: open,
embedded: true,
hideWhenNotConnected: false,
/*fullScreenMode: true,*/
profileAvatar: "assets/fe.jpeg",
openLauncherImage: 'assets/launcher_button.svg',
closeLauncherImage: 'assets/launcher_button.svg',
params: {
images: {
dims: {
width: 300,
height: 200,
}
},
storage: "session"
}
})
</script>
</body>
</html>
这是经过适当修改以支持 https:
后的我的 nginx 配置文件
upstream rasa {
server bot-webchat:5005; # bot-webchat is the chatbot container name
}
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
rewrite ^ https://$host$request_uri? permanent;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
index index.php index.html index.htm;
root /var/www/html;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
location / {
try_files $uri $uri/ =404;
}
# the next two location directives were necessary to avoid mixed content error and allow
# https access to port 5005 and websocket
location /rasa/ {
proxy_pass http://rasa;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location /socket.io/ {
proxy_pass http://rasa/socket.io;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location = /favicon.ico {
log_not_found off; access_log off;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
在我完成测试后,我相信我的 nginx 配置文件可能有问题,但我不知道那是什么......我还使用邮递员来验证“https://example.com/rasa" and "https://example.com/socket.io”正在按预期响应。如果有人能帮我弄清楚发生了什么,我将不胜感激。
我的 bot 也像您一样构建 whit rasa,botfront/wetchat 和 nginx。
webchat的bot配置如下:
WebChat.default.init({
selector: '#webchat',
profileAvatar: 'https://www.jrcg.vip/assets/images/bot_avatar.jpg',
initPayload: '/greet',
params: { storage: 'session' },
customData: { language: 'zh' },
socketUrl: 'https://www.jrcg.vip',
socketPath: '/socket.io/',
title: '五竹',
subtitle: '蠢萌蠢萌的机器人',
inputTextFieldHint: '对我说点什么吧'
});
nginx config中的配置是:
# ...
location /socket.io/ {
proxy_pass http://127.0.0.1:5005;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
# ...
我想你可以将 location /rasa/
和 location /socket.io/
合并到一个位置。
我有一个使用 rasa 实现的聊天机器人,它正在使用 rasa webchat。当我在本地使用 ngrok 测试它时它工作正常,当我在服务器上配置 nginx(端口 80)时它也工作。但是,一旦我更改了 nginx 配置文件以支持 https,网络聊天只显示 "Connecting..." 消息并且没有响应。我在浏览器控制台中查找错误,但有 none。这是我的索引文件,它负责网络聊天界面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fe</title>
</head>
<body>
<div id="webchat">
<script src="https://storage.googleapis.com/mrbot-cdn/webchat-0.5.8.js"></script>
<script>
WebChat.default.init({
selector: "#webchat",
initPayload: "hi",
interval: 1000,
socketUrl: "https://example.com/rasa",
socketPath: "/socket.io/",
title: "speak with bot",
inputTextFieldHint: "say something...",
connectingText: "Connecting...",
connectOn: open,
embedded: true,
hideWhenNotConnected: false,
/*fullScreenMode: true,*/
profileAvatar: "assets/fe.jpeg",
openLauncherImage: 'assets/launcher_button.svg',
closeLauncherImage: 'assets/launcher_button.svg',
params: {
images: {
dims: {
width: 300,
height: 200,
}
},
storage: "session"
}
})
</script>
</body>
</html>
这是经过适当修改以支持 https:
后的我的 nginx 配置文件upstream rasa {
server bot-webchat:5005; # bot-webchat is the chatbot container name
}
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
rewrite ^ https://$host$request_uri? permanent;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
index index.php index.html index.htm;
root /var/www/html;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
location / {
try_files $uri $uri/ =404;
}
# the next two location directives were necessary to avoid mixed content error and allow
# https access to port 5005 and websocket
location /rasa/ {
proxy_pass http://rasa;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location /socket.io/ {
proxy_pass http://rasa/socket.io;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location = /favicon.ico {
log_not_found off; access_log off;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
在我完成测试后,我相信我的 nginx 配置文件可能有问题,但我不知道那是什么......我还使用邮递员来验证“https://example.com/rasa" and "https://example.com/socket.io”正在按预期响应。如果有人能帮我弄清楚发生了什么,我将不胜感激。
我的 bot 也像您一样构建 whit rasa,botfront/wetchat 和 nginx。
webchat的bot配置如下:
WebChat.default.init({
selector: '#webchat',
profileAvatar: 'https://www.jrcg.vip/assets/images/bot_avatar.jpg',
initPayload: '/greet',
params: { storage: 'session' },
customData: { language: 'zh' },
socketUrl: 'https://www.jrcg.vip',
socketPath: '/socket.io/',
title: '五竹',
subtitle: '蠢萌蠢萌的机器人',
inputTextFieldHint: '对我说点什么吧'
});
nginx config中的配置是:
# ...
location /socket.io/ {
proxy_pass http://127.0.0.1:5005;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
# ...
我想你可以将 location /rasa/
和 location /socket.io/
合并到一个位置。