使用ELB和nginx的wss的ActionCable 404错误

时间:2017-04-10 20:20:53

标签: ssl nginx amazon-elb actioncable

当我将网站切换为https时,我收到了actioncable的错误:

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

https网站运行正常,但我获得了一个404的websocket地址。我当前的设置在ELB上终止SSL,nginx将http重定向到https。我和我的铁路服务器一起运行actioncable,而不是独立运行。

我如何设置安全的websockets?

这是我的nginx配置文件

upstream puma {
  server unix://var/run/server.sock;
}

server {
  listen 80;
  server_name default_server;

  root /var/www/apps/server/public;

  location /cable {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_pass http://puma;
  }

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_next_upstream error;

   if ($http_x_forwarded_proto != "https") {
     rewrite ^(.*)$ https://$host$1 permanent;
   }

  proxy_pass http://puma;

  ...

}

以下是ELB上的配置:

ELB Listeners

1 个答案:

答案 0 :(得分:0)

在我的设置中修复了类似错误(actioncable / cable中的404),如下所示

location /cable {
    proxy_pass http://unix:/home/app/shared/tmp/puma/socket;
    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 X-Forwarded-Host $host:$server_port;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;
}

所以在另一个location(X-Real-IP等)中添加相同的proxy_set_header