生产中的ActionCable:WebSocket握手期间出错:意外的响应代码:404

时间:2017-08-02 13:49:44

标签: ruby-on-rails nginx websocket actioncable

在开发模式下,我的本地计算机上的一切正常,但在尝试使用ActionCable的生产服务器上,我得到了:

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

我忘记了什么吗?我在Nginx上运行Puma。

/etc/nginx/nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-enabled/example.com:

upstream myapp_unicorn {
  server localhost:9393;
}


server {
    listen 80;
    server_name example.com;
    return 301 https://example.com/$request_uri;    
}

server {
  server_name example.com;

  listen x.x.x.x:443 ssl http2; # ip censored for SO
  ssl_certificate /etc/letsencrypt/live/example.com-0004/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/example.com-0004/privkey.pem; # managed by Certbot
  include snippets/ssl-params.conf;


  client_max_body_size 50M;
  root /home/myapp/app/myapp/production/current/public;

  location = /webdav { return 404; log_not_found off; }
  location = /apple-touch-icon-120x120.png { return 404; log_not_found off; }
  location = /apple-touch-icon-120x120-precomposed.png { return 404; log_not_found off; }

  location ~ ^/(assets|uploads|images|javascripts|stylesheets|system)/  {
    expires max;
    break;
  }

  location /doc/api {
    alias /home/myapp/app/myapp/production/current/doc/api/;
    try_files $uri $uri/ =404;
  }

  location @myapp {
    proxy_read_timeout 3000;
    proxy_connect_timeout 3000;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
    proxy_pass http://myapp_unicorn;
  }
}

注意:example.com仅适用于此帖子。在我的配置中,我使用了适当的域名。

1 个答案:

答案 0 :(得分:1)

尝试在config / production.rb

中设置配置
  # Mount Action Cable outside main process or domain
  config.action_cable.url = 'wss://example.com/cable'
  config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]