Rails ActionCable in Caprover
With the release of Turbo Stream, there is a good chance that you will use ActionCable as mentioned in previous article. It works well in development, but does not work in production in Caprover. You will see errors in browser console complaining that wss connection is interrupted. Here is the solution.
First, remember to add Redis settings in config/cable.yml
like this:
development:
adapter: asynctest:
adapter: testproduction:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
password: <%= ENV.fetch("REDIS_PASSWORD") {} %>
channel_prefix: prefix
Then add extra settings for Nginx by following this article:
# ActionCable specific
location /cable {
proxy_pass $upstream;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
Be sure it is added in the application specific nginx config under the application settings in Caprover dashboard (select Apps in the side panel then find the application name in main view). This location /cable
need to be the same level (indentation) of location /
. It may take a while to read through the Nginx conf settings. After “Save & Update”, Nginx will reload the new configuration automatically. Errors about interrupted websocket (wss) connection should disappear from browser console. If your application restart repeatly, check your redis credentials. It may be due to error accessing redis server.
There is a Websocket support
in application-specific settings in Caprover. It is useless in this case. It opens up websocket for location /
, not the location /cable
. There is no need to enable it.