NGINX configuration for Rails 5 ActionCable with puma

对着背影说爱祢 提交于 2019-12-18 12:18:58

问题


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.

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.

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.

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

For my application, I followed everything from enter link description here but nothing's mentioned about NGINX configuration

I know that websocket with ActionCable is quite new but I hope someone would be able to give me a lead on that

Many thanks


回答1:


Ok so I finally managed to fix my issue. Here are the different steps which allowed to make this work:

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";
  }
}

And then in my config/environments/development.rb file:

config.action_cable.url = "ws://my.app.com/cable/"

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 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'

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

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



来源:https://stackoverflow.com/questions/34680836/nginx-configuration-for-rails-5-actioncable-with-puma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!