本文介绍了生产中的ActionCable:WebSocket握手期间出错:意外的响应代码:404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在开发模式下,我的本地计算机上的一切正常,但是在尝试使用ActionCable时,在生产服务器上一切正常:
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
我忘记了什么吗?我正在Nginx上运行Puma.
Have I forgot something? I'm running Puma on Nginx.
/etc/nginx/nginx.conf:
/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:
/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
仅用于此帖子.在我的配置中,我使用适当的域.
Note: example.com
is just for this post. In my configuration I user proper domain.
推荐答案
尝试在config/production.rb
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.*/ ]
这篇关于生产中的ActionCable:WebSocket握手期间出错:意外的响应代码:404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!