我已经调查了RoR 5.0.0 ActionCable wss WebSocket handshake: Unexpected response code: 301的答案,但它不适用于我的情况。
我使用一个nginx代理作为几个在docker容器中运行的web服务器的前端。我使用来自https://github.com/jwilder/nginx-proxy的nginx-config-模板。
现在,在我的docker容器中,我有了另一个具有以下配置的nginx:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server my-websocket-docker-container:8080;
}
server {
root /src/html;
location /websocket/ {
resolver 127.0.0.11 ipv6=off;
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
...
}当试图连接到wss://example.com/websocket时,我得到了前面提到的关于意外响应代码301的错误。当我手动卷曲websocket-url时,我可以看到nginx的响应,告诉我"301永久移动“。但是为什么呢?这是从哪里来的?
有人能帮忙吗?谢谢!
发布于 2021-01-12 13:27:36
我今天也面临着一个类似的问题。我使用了“经典”的WebSocket API和NGINX来测试某些服务之间的连接。我想出了以下解决方案:
WebSocket实例创建:
const websocket = new WebSocket('ws://' + window.location.host + '/path');Nginx配置:
location /path {
proxy_pass http://websocket:port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}其中:
https://stackoverflow.com/questions/51802330
复制相似问题