WebSocket握手期间出错:意外的响应代码:502

时间:2019-07-17 19:25:18

标签: ruby-on-rails nginx websocket

我在Digital Ocean上有Rails应用程序(Ununtu 18.04 + nginx + Passenger + Capistrano)。我在基于Action Cable的应用程序中具有聊天功能。在本地,一切正常,但在生产中,我遇到下一个错误:Error during WebSocket handshake: Unexpected response code: 502. 据我了解,动作电缆配置存在问题。但是,一切都按照教程进行,所以我现在真的很困惑,不知道我应该在哪里寻找邪恶。请帮忙! production.rb 文件

config.action_cable.url = '/cable'
config.action_cable.allowed_request_origins = ['http://example.com']

cable.yml文件

production:
adapter: redis
url: redis://redis.exmaple.com:6379

messages.coffee 文件

jQuery(() ->
  App.messages = App.cable.subscriptions.create {channel: 'MessagesChannel', id: $('#conversation_id').val() },
    received: (data) ->
      jQuery('#new_message')[0].reset()
      jQuery('#chat').append data.message 
)

messages_channel.rb 文件

class MessagesChannel < ApplicationCable::Channel
  def subscribed
    stream_from "conversation_#{params[:id]}"
  end
end

/ etc / nginx / sites-enabled / myapp 文件

server {
  listen 80;
  listen [::]:80;

  server_name example.com;
  root /home/deploy/myapp/current/public;

  passenger_enabled on;
  passenger_app_env production;

  location /cable {
    passenger_app_group_name myapp_websocket;
    passenger_force_max_concurrent_requests_per_process 0;
  }

  # Allow uploads up to 100MB in size
  client_max_body_size 100m;

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }
}

在nginx中,我有很多这样的错误:

2019/07/18 07:37:47 [error] 25500#25500: *9088 upstream prematurely closed connection while reading response header from upstream, client: 117.102.40.105, server: example, request: "GET /cable HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.q4FqjUT/agents.s/core:", host: "example.com"

当然到处都有example.com替换为我的真实域名。

1 个答案:

答案 0 :(得分:0)

我发现了问题。它在配置中位于redis url中。在每个教程中,它编写为使用下一个代码:

ERROR 1
ERROR 2 
...

但是看起来它是错误的,正确的将是下一个代码:

ERROR

因此,应将您的域用于localhost。希望这会对别人有所帮助,并且可以节省很多时间)