我正在尝试使用rails 5.0.0beta3和websockets运行应用程序.我在开发本地工作的所有东西,但在生产中我在浏览器的控制台中得到这个响应:
“WebSocket连接失败:WebSocket握手期间出错:意外响应301”
这是我的Nginx conf.
upstream app {
server unix:/home/dev/workspace/my_app/tmp/sockets/thin.0.sock max_fails=1 fail_timeout=15s;
server unix:/home/dev/workspace/my_app/tmp/sockets/thin.1.sock max_fails=1 fail_timeout=15s;
}
server {
listen 443 ssl;
server_name www.my_app.co;
root /home/dev/workspace/my_app/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_next_upstream error timeout http_502 http_503;
proxy_read_timeout 60s;
proxy_pass http://app;
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;
}
location /websocket/ {
proxy_pass http://localhost:28080/;
proxy_http_version 1.1;
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;
ssl on;
ssl_certificate /ssl/www.my_app.co.crt;
ssl_certificate_key /ssl/www.my_app.co.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
}
我正在为我的app服务器运行瘦身,并在本地运行websots服务器和redis.我试图在这里遵循动作电缆示例应用程序:https://github.com/rails/actioncable-examples.
我正在启动我的puma服务器:bundle exec puma -p 28080 cable / config.ru
在配置中使用这个puma.rb:
workers 1
threads 1,10
app_dir = File.expand_path("../..",__FILE__)
shared_dir = "#{app_dir}/shared"
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
stdout_redirect "#{shared_dir}/log/puma.stdout.log","#{shared_dir}/log/puma.stderr.log",true
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
这是我的production.rb配置的相关部分:
config.action_cable.allowed_request_origins = ['https://www.chatben.co','https://45.55.192.195']
config.action_cable.url = "wss://www.chatben.co/websocket"
config.force_ssl = false
这是我的development.rb配置:
config.action_cable.url = "ws://localhost:28080"
config.action_cable.allowed_request_origins = ['http://localhost:3000']
config.action_cable.disable_request_forgery_protection = true
在我的应用程序中,我像这样开始我的电缆:
@App = {}
App.cable = ActionCable.createConsumer()
任何建议都会很棒.我在这里注意到了一个人:RoR 5.0.0 ActionCable wss WebSocket handshake: Unexpected response code: 301
能够通过使用单独的域来解决这个问题.这是我接下来可能会尝试的,但我希望它不会那样.
在此先感谢您的帮助!对此,我真的非常感激.
最佳答案
问题可能是Rails强制你连接到ssl.由于Nginx终止了ssl连接,因此需要设置X-Forwarded-Proto标头以让Rails知道一切正常.这是一个适合我的完整配置:
location /cable {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
}