ruby-on-rails ActionCable -无法在生产中升级到WebSocket

irtuqstp  于 2023-05-30  发布在  Ruby
关注(0)|答案(9)|浏览(262)

ActionCable在生产环境中不起作用。在开发中工作得很好,但在生产中不是。
在Ubuntu 14.04上运行Nginx和Puma。我已经检查了redis-server是启动和运行。
Rails -v 5.0.0.1
production.log

INFO -- : Started GET "/cable/"[non-WebSocket] for 178.213.184.193 at 2016-11-25 14:55:39 +0100
ERROR -- : Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
INFO -- : Finished "/cable/"[non-WebSocket] for 178.213.184.193 at 2016-11-25 14:55:39 +0100

客户请求:

GET ws://mityakoval.com/cable HTTP/1.1
Host: mityakoval.com
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://mityakoval.com
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,uk;q=0.2,nb;q=0.2
Cookie: _vaktdagboka_session=******
Sec-WebSocket-Key: *******
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: actioncable-v1-json, actioncable-unsupported

回复:

HTTP/1.1 404 Not Found
Server: nginx/1.4.6 (Ubuntu)
Date: Fri, 25 Nov 2016 13:52:21 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
X-Request-Id: d6374238-69ef-476e-8fc5-e2f8bbb663de
X-Runtime: 0.002500

nginx.conf

upstream puma {
  server unix:///home/mityakoval/apps/vaktdagboka/shared/tmp/sockets/vaktdagboka-puma.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/mityakoval/apps/vaktdagboka/current/public;
  access_log /home/mityakoval/apps/vaktdagboka/current/log/nginx.access.log;
  error_log /home/mityakoval/apps/vaktdagboka/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  location /cable {
    proxy_pass http://puma;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

cable.yml

redis: &redis
  adapter: redis
  url: redis://127.0.0.1:6379

production: *redis

development:
  adapter: async

test:
  adapter: async

production.rb中:

config.action_cable.allowed_request_origins = ["http://mityakoval.com"]

routes.rb中:

mount ActionCable.server, at: '/cable'

更新:

不要忘记重新启动nginx:)这是我的问题。

8iwquhpp

8iwquhpp1#

您应该将proxy_pass属性的值从http://puma更改为http://puma/cable
因此,/cable的正确location部分为:

location /cable {
  proxy_pass http://puma/cable;
  proxy_http_version 1.1;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "Upgrade";
}
hgc7kmma

hgc7kmma2#

虽然其他帖子已经正确地发布了解决方案,但我认为我应该发布更多关于如何识别问题所在/在哪里修复其他nginx新手的信息。
如果rails错误包含HTTP_UPGRADE:,你会知道你的nginx配置需要在你安装action cable的路径上安装proxy_set_header Upgrade。(这意味着没有任何东西被传递到HTTP_UPGRADE)。修复问题后,我的日志显示HTTP_UPGRADE: websocket

  • Gotchya 1:正如操作中提到的,确保在进行更改后重新启动nginx(我这样做是错误的)。
  • Gotchya 2:也要在nginx配置中查找include语句,因为您的配置可能会被拆分到多个文件中。location /cable {部分应该在server {的内部,在我的例子中,它是缺失的,因为它在一个不同的配置文件中,从一个包含语句,我没有注意到一段时间。
  • 类似的错误,但不同的问题:您的rails日志将在OP提到的origin不允许之前包含一个额外的错误,即当您的rails配置需要更新时,另一个答案提到更新config.action_cable.allowed_request_origins。

日志可能会随着rails而改变,但希望这有助于澄清问题所在,以及我作为一个对nginx一无所知的人遇到的一些问题。

sycxhyv7

sycxhyv73#

但是,对于任何使用Rails 5、Action Cable等的人来说,这个对话已经很晚了。&DEVISE您只需像建议的here那样解决它。这一切都归结为Web Socket服务器没有会话,因此出现了错误消息。

app/channels/application_cable/connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.name
    end

    protected
      def find_verified_user
        verified_user = User.find_by(id: cookies.signed['user.id'])
        if verified_user && cookies.signed['user.expires_at'] > Time.now
          verified_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

app/config/initializers/warden_hooks.rb

Warden::Manager.after_set_user do |user,auth,opts|
  scope = opts[:scope]
  auth.cookies.signed["#{scope}.id"] = user.id
  auth.cookies.signed["#{scope}.expires_at"] = 30.minutes.from_now
end
Warden::Manager.before_logout do |user, auth, opts|
  scope = opts[:scope]
  auth.cookies.signed["#{scope}.id"] = nil
  auth.cookies.signed["#{scope}.expires_at"] = nil
end
  • 解决方案由Greg Molnar开发 *
bxgwgixi

bxgwgixi4#

需要NGINX配置更改的解析才能接受此操作电缆请求

location / {
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

在nginx站点配置中将上述行添加到您的location块中,然后重新启动nginx。

xdnvmnnf

xdnvmnnf5#

我的解决方案是将这些行添加到production.rb文件中:

config.action_cable.url = 'ws://your_site.com/your_action_cable'
  config.action_cable.allowed_request_origins = [ 'http://your_site.com' ]
sq1bmfud

sq1bmfud6#

您可以更改/cable的nginx配置
proxy_set_header X-Forwarded-Proto http;
我用你的nginx配置,并在myu服务器上添加此更改,它工作正常。

tez616oj

tez616oj7#

曾与:

location ^~ /cable {
  ...
}

位置需要^~

8iwquhpp

8iwquhpp8#

我正在使用ws:app-url.com/cable
我使用了这个网址,它工作了,(虽然我已经禁用了电缆的ssl)wss:app-url.com/cable

unguejic

unguejic9#

您的cable.yml文件应该如下所示:

production:
    adapter: redis
    url: <%=ENV['REDIS_URL']%>

然后你应该在环境中设置这个键,应该看起来像这样:

REDIS_URL: 'redis://redistogo:keyblahblahblhblah'

此外,您应该在生产环境中拥有此.rb:

config.web_socket_server_url = "wss://YOUR_URL.com/cable"

相关问题