您好我正在尝试使用ror 5.0.0 beta进行简单聊天(使用puma) 在生产模式上工作(在localhost中没有问题)。
这是我的 Nginx 配置:
upstream websocket {
server 127.0.0.1:28080;
}
server {
listen 443;
server_name mydomain;
ssl_certificate ***/server.crt;
ssl_certificate_key ***/server.key;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers
HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/jenkins.access.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:3000;
proxy_read_timeout 90;
proxy_redirect http://localhost:3000 https://mydomain;
location /cable/{
proxy_pass http://websocket/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
break;
}
}
这是 config / redis / cable.yml
生产: url:redis:// localhost:6379/1
发展: url:redis:// localhost:6379/2
试验: url:redis:// localhost:6379/3
和 config / environments / production.rb
# Action Cable endpoint configuration
config.action_cable.url = 'wss://mydomain/cable'
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = false
这是我收到的错误:
application - [...]。js:27与'wss:// mydomain / cable'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:301
任何提示? :)谢谢
答案 0 :(得分:3)
我解决添加错误乘客。
nginx配置现在是:
server{
listen 80;
passenger_enabled on;
passenger_app_env production;
passenger_ruby /../ruby-2.3.0/ruby;
root /path to application/public;
client_max_body_size 4G;
keepalive_timeout 10;
[...]
location /cable{
passenger_app_group_name websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
}
您必须删除默认文件夹config / redis / cable.yml并将该文件移至/ config。
对于SSL,只需启用默认的ssl选项即可.-)
感谢大家的帮助
答案 1 :(得分:0)
您的websocket URI为/cable/
而不是/cable
,因此后者会点击location /
块。尝试:
location /cable {
rewrite ^/cable$ / break;
rewrite ^/cable(.*)$ $1 break;
proxy_pass http://websocket;
...
}
此外,不确定您是否需要break;
。我认为两个}
块之间缺少location
只是问题中的拼写错误。
EDIT1:添加rewrite
以恢复正确的上游映射。
EDIT2:替代解决方案是明确地将/cable
重写为/cable/
,如下所示:
location = /cable { rewrite ^ /cable/ last; }
location /cable/ {
proxy_pass http://websocket/;
...
}
答案 2 :(得分:0)
我昨天花了将近5个小时试图解决这个特殊问题。我最终为名为ws.example.com
的websocket连接使用了一个单独的域,因为其他所有域都导致301 redirect
。
这是我的nginx.conf
文件。我已删除了SSL部件,但您可以自行插入。请注意,您需要nginx 1.4+,因为此版本之前的所有内容都不支持websocket代理。
upstream socket {
server unix:/mysocket fail_timeout=0;
}
upstream websocket {
server 127.0.0.1:28080;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl;
server_name ws.example.com;
access_log off;
# SSL configs here
location / {
proxy_pass http://websocket/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
server {
listen 443 ssl;
server_name example.com;
# SSL configs here
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://socket;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
我读到allowed_request_origins
没有按预期工作的某个地方,所以我采取了安全的方式(直到修复了错误)并完全使用ActionCable.server.config.disable_request_forgery_protection = true
完成了检查。
这是我的cable.ru
文件,用于启动操作电缆。
require ::File.expand_path('../../config/environment', __FILE__)
Rails.application.eager_load!
require 'action_cable/process/logging'
Rails.logger.level = 0
ActionCable.server.config.disable_request_forgery_protection = true
run ActionCable.server
我还使用了Github的最新rails版本。
gem "rails", github: "rails/rails"