无法使用Action Cable连接websocket

时间:2017-09-29 05:27:33

标签: ruby-on-rails nginx websocket actioncable

无法使用Rails 5.1中的Action Cable连接websocket。 HTTP服务器是nginx上的Unicorn,适配器是Redis。

Rails配置如下。

# config/environments/production.rb
config.action_cable.disable_request_forgery_protection = true

nginx配置如下。

upstream unicorn {
  server unix:/rails/current/tmp/sockets/unicorn.sock;
}

server {
  listen 80;
  charset utf-8;
  server_name sub.example.com;
  root /rails/current/public;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
      proxy_pass http://unicorn;
      break;
    }
  }
  location /cable {
    proxy_pass http://unicorn;
    proxy_http_version 1.1;
    proxy_set_header Upgrade "websocket";
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_redirect off;
  }
  ...
}

Web浏览器控制台中的错误如下:

WebSocket connection to 'wss://sub.example.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404

Rails中的错误如下。

[ERROR] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
[INFO] Finished "/cable/"[non-WebSocket] for xxx.xxx.xxx.xxx at 2017-xx-xx

最奇怪的是Rails中的错误没有HTTP_UPGRADE。但是,Web浏览器的HTTP请求标头包含Upgrade密钥和websocket值。还要在nginx配置中为代理标头设置"websocket"

我该怎么办?

1 个答案:

答案 0 :(得分:0)

  

在该文章的背面,我刚刚意识到通用的解决方案是   一个简单的更改config / secrets.yml文件来引用   ENV [" PORT"]设置   ...

11:06:55 21 DRMWorkaround.maybeDisableExportControls: Failed to deactivate AES-256 barrier logic, Tor mode/BIP38 decryption may crash if this JVM requires it: Can not set static final boolean field javax.crypto.JceSecurity.isRestricted to (boolean)false 11:06:56 21 AbstractBlockChain.addWallet: Wallet/chain height mismatch: -1 vs 1147104 11:06:56 21 AbstractBlockChain.addWallet: Hashes: null vs 0000000000000a1c34e39a6ff2c396c9c4cede41897c7475b47438ef94a7e6e0 11:06:56 25 PeerGroup$8.run: Starting ... 11:06:56 25 PeerGroup$8.run: Starting Tor/Orchid ... 11:06:56 25 TorClient.start: Starting Orchid (version: 1.0.0) 11:06:56 26 DirectoryImpl.loadFromStore: Loading cached network information from disk 11:06:56 26 DirectoryImpl.loadFromStore: Loading certificates 11:06:56 27 CircuitCreationTask.checkCircuitsForCreation: Cannot build circuits because we don't have enough directory information 11:06:56 26 DirectoryImpl.loadFromStore: Loading consensus 11:06:56 26 ConsensusDocumentImpl.verifySingleAuthority: Consensus signed by unrecognized directory authority: 0232af901c31a04ee9848595af9bb7620d4c5b2e 11:06:56 26 DirectoryImpl.loadFromStore: Loading microdescriptor cache 11:06:57 26 DirectoryImpl.loadFromStore: loading state file 11:06:59 20 KeyCrypterScrypt.deriveKey: Deriving key took 4.364 s for 32768 scrypt iterations. 11:06:59 20 WalletSetPasswordController.lambda$0: Background test key derivation took 4367msec 11:07:22 29 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:22 32 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:22 30 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:22 28 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:22 31 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:48 32 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:52 30 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:52 31 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:52 28 DirectoryDownloadTask$DownloadRouterDescriptorsTask.run: Failed to download router descriptors: Failed to open directory circuit 11:07:56 25 PeerGroup$8.run: Exception when starting up java.lang.RuntimeException: java.util.concurrent.TimeoutException at org.bitcoinj.core.PeerGroup$8.run(PeerGroup.java:1146)

# Be sure to restart your server when you modify this file.

  

您也可以在点文件中添加:

     

export PORT = 3000

source