gpt4 book ai didi

ruby-on-rails - ActionCable Websocket 升级和 404 错误

转载 作者:行者123 更新时间:2023-12-05 07:28:10 24 4
gpt4 key购买 nike

我一直在尝试在 AWS、经典负载均衡器、Elastic Bean Stalk 环境(乘客 Amazon 64 位 Linux 2.7.2、Postgresql)中使用 Rails 5.2 应用程序在 Ruby (2.5) 中获取 ActionCable。起初,Redis 不工作,所以我将适配器切换到 postgresql,它修复了很多页面错误,除了我目前拥有的两个。聊天应用程序运行正常,但发送和接收消息的最重要部分没有刷新。 ActionCable 在开发中工作得很好,我没有收到任何错误。我在日志中收到一个错误,在浏览器控制台上收到一个重复错误。

以下是 Chrome 上的错误,每隔几秒就会出现一次。

application-d192ecfa7b18a37710113f3ff55fa53e559048f4f74047c1ac2895013f588b6e.js:22 WebSocket connection to 'wss://www.my-app.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404

同样在日志中,我收到此错误:

E, [2018-12-01T00:09:22.806141 #8733] ERROR -- : [92c25112-2d7b-4ae5-8567-73f9f1eb73be] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, HTTP_UPGRADE: )

以下是 ActionCable 的相关文件。我尝试了很多故障排除和不同的建议。我试图修改 nginx.conf 文件,但每当我放置任何与 passenger_* 相关的指令时,一些消息来源推荐的指令,重新启动 NGINX 都会给我一个错误,提示 [emerg] unknown directive。我尝试了其中的其他解决方案,例如指定 location/cable,大量的指南、教程和 SO 帖子,我在其中实现并尝试了每一个:ActionCable - Failed to upgrade to WebSocket in production

电缆.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' ]

电缆.js

(function() {
this.App || (this.App = {});

App.cable = ActionCable.createConsumer("/cable");

}).call(this);

路线.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] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied) 2018/12/10 20:33:52 [warn] 10587#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5 2018/12/10 20:33:52 [warn] 10587#0: conflicting server name "localhost" on 0.0.0.0:80, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 2018/12/10 20:33:52 [emerg] 10587#0: open() "/var/run/nginx.pid" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed

非常感谢您的阅读和任何帮助。

更新

在 Reddit 用户的帮助下(这里是链接:reddit post)我能够解决无法升级 Websocket 错误和 404 握手错误响应。这是通过删除上面的 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 [error] 7640#0: *533 connect() to unix:///var/run/puma/my_app.sock failed (111: Connection refused) while connecting to upstream, client: 172.31.33.196, server: , request: "GET / HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/", host: "172.31.11.174"

再次感谢!

最佳答案

2018/12/10 20:05:02 [error] 7640#0: *533 connect() to unix:///var/run/puma/my_app.sock failed 
(111: Connection refused) while connecting to upstream,
client: 172.31.33.196, server: , request: "GET / HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/", host: "172.31.11.174"

这个错误是因为上游没有找到my_app.sock文件。您只需将 proxy_pass 中的 backend 替换为 ip:port ,错误就会消失。例如:

proxy_pass http://132.197.231.16:3000;

关于ruby-on-rails - ActionCable Websocket 升级和 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53566797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com