Actioncable - Failed to Upgrade to Websocket in Production

Rails 6 ActionCable Unable to Upgrade WebSocket Request

After posting on reddit, I was able to fix my issue by:

  1. Removing my .ebextensions/nginx_proxy.config file.
  2. Creating a new file, .platform/nginx/conf.d/elasticbeanstalk/websocket.conf with the contents:
location /cable {
proxy_pass http://my_app/cable;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Rails 6 Production ActionCable Error during WebSocket handshake

After a few days, I managed to solve this problem myself.
The main reason for my error is that in my environments / production.rb file, I was saying that the actioncable endpoint was the public ip of my ec2. But in reality you should put localhost. I used the same configuration I have in development.rb:

production.rb before:

...
config.action_cable.mount_path = '/cable'
config.action_cable.url = 'ws://my_ec2/cable'
config.action_cable.allow_same_origin_as_host = true
config.action_cable.allowed_request_origins = ["*"]
...

production.rb after:

...
config.action_cable.disable_request_forgery_protection = true
config.action_cable.url = "ws://localhost:3000/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
config.action_cable.allowed_request_origins = /(\.dev$)|^localhost$/
...

Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: , HTTP_UPGRADE: )

May be you need some config in your nginx configuration? I do not really worked with passenger, but in my project I use it like:

  upstream socket_server {
server <your_ip>:<your_port>;
}

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

I use anycable gem, with golang socket server, and it may have differences with passenger and default actioncable but may be my answer can help to rid of your problem

(Also, I strongly recommend to move on anycable because it is consumes the least server resources:))



Related Topics



Leave a reply



Submit