【发布时间】:2019-05-03 03:44:33
【问题描述】:
我一直在尝试在 Rails 5.2 应用程序上的 Ruby (2.5) 中获取 ActionCable,该应用程序在 AWS、经典负载均衡器、Elastic Bean Stalk 环境(乘客 Amazon 64 位 Linux 2.7.2、Postgresql)中进行生产。起初,Redis 没有工作,所以我将适配器切换到 postgresql,它修复了很多页面错误,除了我目前拥有的两个错误。聊天应用程序运行正常,但最重要的部分是无需刷新即可发送和接收消息。 ActionCable 在开发中工作得很好,我没有收到任何错误。我在日志中收到一个错误,在浏览器控制台上收到一个重复错误。
以下是 Chrome 上的错误,每隔几秒就会出现一次。
application-d192ecfa7b18a37710113f3ff55fa53e559048f4f74047c1ac2895013f588b6e.js:22 WebSocket 连接到“wss://www.my-app.com/cable”失败:WebSocket 握手期间出错:意外响应代码:404
同样在日志中,我收到此错误:
E, [2018-12-01T00:09:22.806141 #8733] 错误 -- : [92c25112-2d7b-4ae5-8567-73f9f1eb73be] 无法升级到 WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, HTTP_UPGRADE :)
以下是 ActionCable 的相关文件。我已经尝试了很多故障排除和不同的建议。我试图修改 nginx.conf 文件,但是每当我放置任何与乘客_* 相关的指令时,一些消息来源推荐,重新启动 NGINX 会给我一个错误,提示 [emerg] 未知指令。我在其中尝试了其他解决方案,例如指定 location /cable,大量的指南、教程和类似这样的帖子,我在其中实现并尝试了每一个:ActionCable - Failed to upgrade to WebSocket in production
cable.yml
development:
adapter: postgresql
test:
adapter: async
production:
adapter: postgresql
channel_prefix: production
生产.rb
config.action_cable.mount_path = '/cable'
config.action_cable.url = 'wss://mywebsite.com/cable'
config.action_cable.allowed_request_origins = [ 'http://mywebsite.com', 'https://mywebsite.com' ]
cable.js
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer("/cable");
}).call(this);
routes.rb
mount ActionCable.server => '/cable'
application_cable/connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_admin
def connect
self.current_admin = find_verified_user
logger.add_tags "ActionCable", "Admin: #{current_admin.email}"
end
protected
def find_verified_user
verified_user = Admin.find_by(id: cookies.signed['admin.id'])
if verified_user
verified_user
else
reject_unauthorized_connection
end
end
end
end
请参阅下面的 nginx 配置更新。
运行 nginx -t 后,读数如下:
nginx:[alert] 无法打开错误日志文件:open() “/var/log/nginx/error.log”失败(13:权限被拒绝)2018/12/10 20:33:52 [警告] 10587#0:“用户”指令只有在 主进程以超级用户权限运行,在 /etc/nginx/nginx.conf:5 2018/12/10 20:33:52 [警告] 10587#0: 0.0.0.0:80 上的冲突服务器名称“localhost”,忽略 nginx: 配置文件 /etc/nginx/nginx.conf 语法没问题 2018/12/10 20:33:52 [emerg] 10587#0: open() "/var/run/nginx.pid" 失败 (13: 权限被拒绝)nginx:配置文件/etc/nginx/nginx.conf 测试失败
非常感谢您的阅读和任何帮助。
更新
在 Reddit 用户的帮助下,(这里是链接:reddit post)我能够解决 Failed to upgrade Websocket 错误和 404 Handshake Error 响应。这是通过删除我上面的 nginx.conf 配置并将其添加到 nginx/conf.d 文件夹中的 default.conf 文件来完成的。下面是我的新配置。
upstream backend {
server unix:///var/run/puma/my_app.sock;
}
server {
listen 80;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
large_client_header_buffers 8 32k;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_pass http://backend;
proxy_redirect off;
location /assets {
root /var/app/current/public;
}
# enables WS support
location /cable {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection Upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
现在我的 production.log 或浏览器控制台中没有错误。但是,我仍然有那些 nginx 测试错误,并且 nginx/error.log 中仍然存在错误。该错误如下:
2018/12/10 20:05:02 [错误] 7640#0: *533 connect() 到 unix:///var/run/puma/my_app.sock 失败(111:连接被拒绝) 连接上游时,客户端:172.31.33.196,服务器:, 请求:“GET / HTTP/1.1”,上游: “http://unix:///var/run/puma/my_app.sock:/”,主机:“172.31.11.174”
再次感谢!
【问题讨论】:
-
您好,遇到同样的错误,您找到解决方案了吗?
-
对不起,DIv。我没有 :( 但这是我开始的一个 Reddit 线程,可能会有所帮助。reddit.com/r/rails/comments/a44ji1/…
标签: ruby-on-rails amazon-web-services websocket passenger actioncable