问题
Everything works fine on my local machine in development mode but on production server when trying to use ActionCable I get:
WebSocket connection to 'wss://example.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404
Have I forgot something? I'm running Puma on Nginx.
/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;
}
}
Note: example.com
is just for this post. In my configuration I user proper domain.
回答1:
Try to set up configs in 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.*/ ]
来源:https://stackoverflow.com/questions/45462800/actioncable-on-production-error-during-websocket-handshake-unexpected-response