gpt4 book ai didi

带有 puma 的 Rails 5 ActionCable 的 NGINX 配置

转载 作者:行者123 更新时间:2023-12-03 21:52:26 27 4
gpt4 key购买 nike

我正在将 Jelastic 用于我的开发环境(尚未投入生产)。
我的应用程序正在使用 Unicorn 运行,但我发现了带有 ActionCable 的 websockets 并将其集成到我的应用程序中。

在本地一切正常,但是当部署到我的 Jelastic 环境(使用默认的 NGINX/Unicorn 配置)时,我在我的 javascript 控制台中收到此消息,但我在访问日志中看不到任何内容

WebSocket connection to 'ws://dev.myapp.com:8080/' failed: WebSocket is closed before the connection is established.

我曾经在我的本地环境中使用过,我通过在我的配置文件中添加所需的 ActionCable.server.config.allowed_request_origins 来解决它。所以我为此仔细检查了我的开发配置,没问题。

这就是为什么我想知道是否有针对 NGINX 配置的特定内容,而不是 ActionCable git 页面上解释的内容
bundle exec puma -p 28080 cable/config.ru

对于我的申请,我遵循了 enter link description here 中的所有内容。但没有提到 NGINX 配置

我知道带有 ActionCable 的 websocket 是很新的,但我希望有人能给我一个指导

非常感谢

最佳答案

好的,所以我终于设法解决了我的问题。以下是允许此工作的不同步骤:

1.nginx :我真的不知道这是否需要,但是当我的应用程序与 Unicorn 一起运行时,我将它添加到我的 nginx conf 中

upstream websocket {
server 127.0.0.1:28080;
}

server {
location /cable/ {
proxy_pass http://websocket/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}

然后在我的 config/environments/development.rb文件:
config.action_cable.url = "ws://my.app.com/cable/"

2.允许请求来源:我注意到即使我使用 ActionCable.server.config.allowed_request_origins 我的连接也被拒绝了在我的 config/environments/development.rb文件。我想知道这是否是由于开发默认设置为 http://localhost:3000如文档中所述。所以我添加了这个:
ActionCable.server.config.disable_request_forgery_protection = true

我还没有生产环境,所以我还不能测试它将如何。

3.Redis 密码:如文档中所述,我使用的是 config/redis/cable.yml但我遇到了这个错误:
Error raised inside the event loop: Replies out of sync: #<RuntimeError: ERR operation not permitted>
/var/www/webroot/ROOT/public/shared/bundle/ruby/2.2.0/gems/em-hiredis-0.3.0/lib/em-hiredis/base_client.rb:130:in `block in connect'

所以我明白我为我的 redis 服务器设置密码的方式并不好。

事实上,你必须做这样的事情:
development:
<<: *local
:url: redis://user:password@my.redis.com:6379
:host: my.redis.com
:port: 6379

现在一切正常,Actioncable 确实令人印象深刻。

也许我的一些问题是微不足道的,但我正在分享它们以及我如何解决它们,以便每个人都可以在需要时选择一些东西

关于带有 puma 的 Rails 5 ActionCable 的 NGINX 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680836/

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