【问题标题】:ActionCable with SSL not working in production environment带有 SSL 的 ActionCable 在生产环境中不起作用
【发布时间】:2019-10-30 14:52:26
【问题描述】:

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

我的 ActionCable 网址是:

SOCKET_URL: wss://example.com/cable
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

我的 production.rb 文件中的配置是:

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

我的 nginx 配置是:

upstream unicorn {
    server unix:/usr/share/nginx/html/demo_app/shared/tmp/unicorn.demo_app.sock fail_timeout=0;
}

server {
        listen 80;
        server_name example.com;
        root /usr/share/nginx/html/demo_app/current/public;

        try_files $uri/index.html $uri @unicorn;

        location @unicorn {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto https;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://unicorn;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
        }

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

【问题讨论】:

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


    【解决方案1】:

    您可以在 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";
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多