简体   繁体   中英

Does Rails with Action Cable run with NGiNX proxying in front of Puma?

I am aware that there are 1-2 other questions about this theme - but none of them seems to cover my angle or offer a solution;

My browser console says:

WebSocket connection to 'wss://sucker.alco.dk/cable' failed: WebSocket is closed before the connection is established.

I would have settled for that easily if only the 'cable' did work at all :( Working in development had me rush to the bank, but alas while in production things are not as shiny I'm afraid :(

My Ruby is fairly old - 2.1.2p95 - my Rails somewhat newer:

# Gemfile
gem 'rails', '~> 5.1.6', '>= 5.1.6.2'
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'redis', '~> 4.0'

I've added an issue on github.com/puma/puma but as other members of that community point out, they are running the almost exact same setups - with success - so they are kindly pointing the finger at Rails; I'm mostly thinking that I miss some stupid detail :$

There's a demo project at github.com/wdiechmann/sucker if you're up for some debugging - but a kind word, a wet handkerchief on my forehead, or utter silence - I'm not picky ;)

EDIT on 24/5/2019

My production.log does not look "intimidated" in any way - all I's

I, [2019-05-24T07:19:01.182723 #21335]  INFO -- : [9855de1f-65b0-41d5-bdd1-726dad5a875e] Started GET "/cable" for 92.246.28.33 at 2019-05-24 07:19:01 +0200
I, [2019-05-24T07:19:01.183340 #21335]  INFO -- : [9855de1f-65b0-41d5-bdd1-726dad5a875e] Started GET "/cable/" [WebSocket] for 92.246.28.33 at 2019-05-24 07:19:01 +0200
I, [2019-05-24T07:19:01.183425 #21335]  INFO -- : [9855de1f-65b0-41d5-bdd1-726dad5a875e] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-24T07:19:11.398314 #21335]  INFO -- : Finished "/cable/" [WebSocket] for 92.246.28.33 at 2019-05-24 07:19:11 +0200
I, [2019-05-24T07:19:12.545945 #21335]  INFO -- : [6d9a5967-57c7-45bf-b99e-2f3d74ba72f6] Started GET "/cable" for 92.246.28.33 at 2019-05-24 07:19:12 +0200
I, [2019-05-24T07:19:12.546559 #21335]  INFO -- : [6d9a5967-57c7-45bf-b99e-2f3d74ba72f6] Started GET "/cable/" [WebSocket] for 92.246.28.33 at 2019-05-24 07:19:12 +0200
I, [2019-05-24T07:19:12.546677 #21335]  INFO -- : [6d9a5967-57c7-45bf-b99e-2f3d74ba72f6] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-24T07:19:22.914389 #21335]  INFO -- : Finished "/cable/" [WebSocket] for 92.246.28.33 at 2019-05-24 07:19:22 +0200

But I like to believe that I miss at least a couple lines of 'log' (like how it shows in my development.log)

Started GET "/cable" for ::1 at 2019-05-24 07:39:47 +0200
Started GET "/cable/" [WebSocket] for ::1 at 2019-05-24 07:39:47 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
MessageChannel is transmitting the subscription confirmation
MessageChannel is streaming from message_channel

- and I'd really like my DevTools console to "shut up" - and the channel to actually work, obviously :$

if you're using https , add the following in your nginx site's configuration:

location /cable {
  proxy_pass http://app;
  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 $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-Proto https;
  proxy_redirect off;
}

This is tested and working perfectly :-)

I've spent nearly 40hrs barking up almost every other 'tree in town' :)

The (short) answer to this question is: yes!

The (somewhat) longer answer: Be very careful to be in full control of both your hardware and/or virtual machine (in my example I was limping along on an old Debian distro, patched a million times with all sorts of stuff, running nearly 10 other applications each with his set of 'specials'!

My advice: Go with a fresh box for your first shot - when you know the tech and the intricacies and feel adventurous go seek that old Slackware distro from '98 and commit to your heart's content :)

The (really long) answer: clone https://github.com/wdiechmann/sucker.git for some initial tyre kicking

Cheers, Walther

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM