【问题标题】:Does Rails with Action Cable run with NGiNX proxying in front of Puma?带有 Action Cable 的 Rails 是否在 Puma 前使用 NGiNX 代理运行?
【发布时间】:2019-05-23 18:48:30
【问题描述】:

我知道还有 1-2 个关于这个主题的其他问题 - 但似乎没有一个涵盖我的角度或提供解决方案;

我的浏览器控制台说:

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

如果只有“电缆”能正常工作,我会很容易接受的 :( 在开发中工作让我急于去银行,但是在生产中,我担心事情没有那么闪亮:(

我的 Ruby 相当老 - 2.1.2p95 - 我的 Rails 有点新:

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

我在 github.com/puma/puma 上添加了一个问题,但正如该社区的其他成员所指出的那样,他们正在运行 几乎完全相同的设置 - 成功 - 所以他们将矛头指向 Rails;我主要在想 我错过了一些愚蠢的细节:$

github.com/wdiechmann/sucker 有一个演示项目,如果您准备进行一些调试 - 但一句好话,一个湿 手帕在我的额头上,或者完全沉默——我不挑剔;)

于 2019 年 5 月 24 日编辑

我的 production.log 看起来并没有任何“被吓倒”的感觉 - 所有 我的

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

但我愿意相信我至少错过了几行“日志”(就像它在我的 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

- 我真的很希望我的 DevTools 控制台“闭嘴”- 显然,该频道可以正常工作:$

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 actioncable


    【解决方案1】:

    如果您使用的是https,请在您的nginx 站点的配置中添加以下内容:

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

    这已经过测试并且运行良好:-)

    【讨论】:

    • 是的 - 好吧 - 人们一直在告诉我 ;) (如果你看看这个傻瓜项目 - 这就是我设置的 - 现在我什至尝试删除 proxy_set_header X-Forwarded-For... 左边的最后一条评论- 唉,无济于事
    【解决方案2】:

    我已经花了将近 40 小时的时间在几乎所有其他“城里的树”上吠叫 :)

    这个问题的(简短)答案是:是的!

    (有点)更长的答案:要非常小心地完全控制您的硬件和/或虚拟机(在我的示例中,我在一个旧的 Debian 发行版上一瘸一拐,用各种各样的东西修补了一百万次,运行近 10 个其他应用程序,每个应用程序都有自己的“特价商品”!

    我的建议:第一次拍摄时带一个新盒子 - 当您了解技术和复杂性并喜欢冒险时,去寻找 98 年的旧 Slackware 发行版,尽情享受吧 :)

    (真的很长)答案:克隆https://github.com/wdiechmann/sucker.git 以获得一些初始tyre kicking

    干杯, 瓦尔特

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-28
      • 2019-12-30
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2015-10-17
      • 2014-10-03
      相关资源
      最近更新 更多