带PUMA的Rails 5 ActionCable的NGINX配置 [英] NGINX configuration for Rails 5 ActionCable with puma
问题描述
我正在将Jelastic用于我的开发环境(尚未投入生产). 我的应用程序与Unicorn一起运行,但是我发现了带有ActionCable的websocket,并将其集成到我的应用程序中.
I am using Jelastic for my development environment (not yet in production). My application is running with Unicorn but I discovered websockets with ActionCable and integrated it in my application.
在本地,一切正常,但是当部署到我的Jelastic环境(使用默认的NGINX/Unicorn配置)时,我在javascript控制台中收到了此消息,并且访问日志中没有任何内容
Everything is working fine in local, but when deploying to my Jelastic environment (with the default NGINX/Unicorn configuration), I am getting this message in my javascript console and I see nothing in my access log
WebSocket connection to 'ws://dev.myapp.com:8080/' failed: WebSocket is closed before the connection is established.
我曾经在本地环境中使用,我通过在配置文件中添加所需的ActionCable.server.config.allowed_request_origins解决了该问题.因此,我为此仔细检查了开发配置,没关系.
I used to have on my local environment and I solved it by adding the needed ActionCable.server.config.allowed_request_origins in my config file. So I double-checked my development config for this and it is ok.
这就是为什么我想知道是否有NGINX配置的特定内容,而不是ActionCable git页面上解释的内容
That's why I was wondering if there is something specific for NGINX config, else than what is explained on ActionCable git page
bundle exec puma -p 28080 cable/config.ru
对于我的应用程序,我遵循了在此处输入链接描述中的所有内容,但没有提及有关NGINX配置的内容
For my application, I followed everything from enter link description here but nothing's mentioned about NGINX configuration
我知道带有ActionCable的websocket是相当新的,但我希望有人能够在此方面带头我
I know that websocket with ActionCable is quite new but I hope someone would be able to give me a lead on that
非常感谢
推荐答案
好,所以我终于设法解决了这个问题.以下是可以完成这项工作的不同步骤:
Ok so I finally managed to fix my issue. Here are the different steps which allowed to make this work:
1.nginx:我真的不知道是否需要这样做,但是当我的应用程序与Unicorn一起运行时,我将其添加到了我的nginx conf中
1.nginx : I don't really know if this is needed but as my application is running with Unicorn, I added this into my 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.允许请求来源:然后我注意到,即使我在config/environments/development.rb
文件中使用ActionCable.server.config.allowed_request_origins
,我的连接也被拒绝了.我想知道这是否不是由于文档中所述的开发默认为 http://localhost:3000 而导致的.所以我添加了这个:
2.Allowed request origin: I have then noticed that my connection was refused even if I was using ActionCable.server.config.allowed_request_origins
in my config/environments/development.rb
file. I am wondering if this is not due to the development default as http://localhost:3000 as stated in the documentation. So I have added this:
ActionCable.server.config.disable_request_forgery_protection = true
我还没有生产环境,所以我还无法测试它的状态.
I have not yet a production environment so I am not yet able to test how it will be.
3.Redis密码:如文档中所述,我使用的是config/redis/cable.yml
,但出现此错误:
3.Redis password: as stated in the documentation, I was using a config/redis/cable.yml
but I was having this error:
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服务器设置密码的方式不好.
So I understood the way I was setting my password for my redis server was not good.
实际上,您必须执行以下操作:
In fact your have to do something like this:
development:
<<: *local
:url: redis://user:password@my.redis.com:6379
:host: my.redis.com
:port: 6379
现在一切正常,Actioncable确实令人印象深刻.
And now everything is working fine and Actioncable is really impressive.
也许我的一些问题是微不足道的,但是我正在分享它们以及我如何解决这些问题,以便每个人都可以根据需要选择一些东西
Maybe some of my issues were trivial but I am sharing them and how I resolved them so everyone can pick something if needed
这篇关于带PUMA的Rails 5 ActionCable的NGINX配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!