带SSL的ActionCable在生产环境中不起作用

时间:2019-10-30 14:52:26

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

我正在Rails 5.2中使用ActionCable,并将代码部署在具有nginx服务器的AWS上。 以前,当我使用http时,WebSocket可以工作,但是当我实现SSL时,它就停止了工作。对于SSL,我已经在AWS中实现了负载均衡器。我正在使用Unicorn作为Rails应用程序服务器。

我的ActionCable网址是:

filename = my_csv_file
account_name = <blob_storage>
account_key = <blob_key>

block_blob_service = BlockBlobService(account_name, account_key)
blobstring = block_blob_service.get_blob_to_text(<input_container_name>, filename).content
SOCKET_URL: wss://example.com/cable
我的production.rb文件中的

配置是:

Started GET "/cable/"[non-WebSocket] for 182.74.85.106 at 2019-10-30 14:32:05 +0000
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 182.74.85.106 at 2019-10-30 14:32:05 +0000

我的nginx conf是:

config.action_cable.url = ENV["SOCKET_URL"]
ActionCable.server.config.disable_request_forgery_protection = true

1 个答案:

答案 0 :(得分:0)

您可以将以下行粘贴到nginx.conf中,然后尝试。

location /cable {  
    proxy_pass http://unicorn/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";
}