Hotwire websocket connection failed for Rails6 + Nginx and Phusion Passenger 6.0.4

Viewed 146

I'm trying to use Hotwire for Rails, it works perfectly with my development environment, but having issues with websocket connection after deploy to our UAT (staging) environment.

Stack

Ruby: 3.0.0

Rails: 6.0.3.5

Nginx: 1.14.1

Passenger: Phusion Passenger 6.0.4

nginx.conf on my Gateway server

upstream myapp {
    server myapp.uat.mycompany.com.au:80;
}

server {
    server_name myapp.uat.mycompany.com.au;

    location / {
        proxy_pass http://myapp;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

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

    listen 443 ssl; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myapp.uat.mycompany.com.au/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.uat.mycompany.com.au/privkey.pem; # managed by Certbot

}

server {
    if ($host = myapp.uat.mycompany.com.au) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    server_name myapp.uat.mycompany.com.au;
    listen 80;
    return 404; # managed by Certbot
}

nginx.conf on my app server

server {
  server_name myapp.uat.mycompany.com.au;
  listen 80;
  underscores_in_headers on;
  passenger_enabled on;
  passenger_app_env uat;
  root /var/www/myapp/current/public;

  location /cable {
     passenger_app_group_name myapp_action_cable;
     passenger_force_max_concurrent_requests_per_process 0;
  }
}

cable.yml

 development:
      adapter: redis
      url: <%= ENV.fetch("ACTION_CABLE_URL") { "redis://localhost:6379/8" } %>
      channel_prefix: myapp

    test:
      adapter: test

    uat:
      adapter: redis
      url: <%= ENV.fetch("ACTION_CABLE_URL") { "redis://localhost:6379/8" } %>
      channel_prefix: myapp

    production:
      adapter: redis
      url: <%= ENV.fetch("ACTION_CABLE_URL") { "redis://localhost:6379/8" } %>
      channel_prefix: myapp

uat.rb (environment configuration file)

config.action_cable.url = 'wss://myapp.uat.mycompany.com.au/cable'
config.action_cable.allowed_request_origins = ['https://myapp.uat.mycompany.com.au', 'http://myapp.uat.mycompany.com.au']

Errors displayed in Chrome console

connection.js:70 WebSocket connection to 'wss://myapp.uat.mycompany.com.au/cable' failed: WebSocket is closed before the connection is established.

Errors in uat.log

I, [2021-10-29T15:13:33.876173 #668]  INFO -- : [25f60292-8a89-4ce5-9f35-fb3ad56bef00] Started GET "/cable" for 10.1.32.123 at 2021-10-29 15:13:33 +1100
I, [2021-10-29T15:13:33.876890 #668]  INFO -- : [25f60292-8a89-4ce5-9f35-fb3ad56bef00] Started GET "/cable/" [WebSocket] for 10.1.32.123 at 2021-10-29 15:13:33 +1100
I, [2021-10-29T15:13:33.876966 #668]  INFO -- : [25f60292-8a89-4ce5-9f35-fb3ad56bef00] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
E, [2021-10-29T15:13:33.877255 #668] ERROR -- : [25f60292-8a89-4ce5-9f35-fb3ad56bef00] WebSocket error occurred: wrong number of arguments (given 2, expected 1)
E, [2021-10-29T15:13:33.878046 #668] ERROR -- : WebSocket error occurred: wrong number of arguments (given 2, expected 1)
E, [2021-10-29T15:13:34.234885 #668] ERROR -- : WebSocket error occurred: wrong number of arguments (given 2, expected 1)
E, [2021-10-29T15:13:36.397514 #20846] ERROR -- : WebSocket error occurred: wrong number of arguments (given 2, expected 1)

Note: the above WebSocket error keeps happening ...

I googled around and found someone had the similar problem, so tried with the solution of commenting out the config.action_cable.url in the uat.rb environment file and put action_cable_meta_tag in the application.html.slim file, but it didn't work.

I've spent lots of time with it, could someone shed some light on this please?

1 Answers

It works fine in my production environment. The only difference I can see is that the passenger version is Phusion Passenger 6.0.4 in my production env.

Will try to upgrade my staging passenger version

Related