我试图将Rails 5.0.1应用于运行Ubuntu,Nginx和Puma的生产服务器,并使用Capistrano。该应用程序曾经运行得很好,直到我们添加可动感为止。所有内容(包括动作效果(在开发环境中完美运行,但是当应用程序被部署到生产时,动作效果不会连接。
我遵循本教程以获取基本设置...
问题:
问题是,每次浏览器尝试websocket握手时,请求是301重定向...
浏览器JavaScript控制台反馈:
WebSocket connection to 'wss://app.example.com/cable' failed: Unexpected response code: 301
nginx访问日志反馈:
XXX.XXX.XXX.XXX - - [07/Jun/2017:17:16:22 +0800] "GET /cable HTTP/1.1" 301 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4"
$ curl -v https://app.example.com/cable
:
* Trying XXX.XXX.XXX.XXX...
* TCP_NODELAY set
* Connected to app.example.com (XXX.XXX.XXX.XXX) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: app.example.com
* Server certificate: COMODO RSA Domain Validation Secure Server CA
* Server certificate: COMODO RSA Certification Authority
> GET /cable HTTP/1.1
> Host: app.example.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.4.6 (Ubuntu)
< Date: Wed, 07 Jun 2017 11:42:27 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: https://puma/cable
<
* Curl_http_done: called premature == 0
* Connection #0 to host app.example.com left intact
设置:
nginx.conf
:
upstream puma {
server unix:///home/deploy/apps/appname/shared/tmp/sockets/appname-puma.sock;
}
server { # Primary server block
# Bind port(s)
listen 80;
# listen [::]:80;
listen 443 ssl;
# Bind domain(s)
server_name app.example.com;
# Bind certificate(s)
ssl_certificate /etc/nginx/ssl/app.example.com/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/app.example.com/app.example.com.key;
root /home/deploy/apps/appname/current/public;
access_log /home/deploy/apps/appname/current/log/nginx.access.log;
error_log /home/deploy/apps/appname/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
# ActionCable
location /cable {
proxy_pass http://puma;
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 X-Forwarded-Proto $scheme;
}
try_files $uri/index.html $uri @puma;
location @puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# Let Rails see current protocol
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
#error_page 503 /503.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
cable.yml
:
development:
adapter: async
test:
adapter: async
production:
adapter: redis
url: redis://localhost:6379/1
如果更多信息会有所帮助,请告诉我!
任何建议都将不胜感激。
谢谢
有相同的问题,最终从这里复制了几个指令:
location /cable {
proxy_pass http://puma;
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;
}
重新启动nginx和puma,它开始工作。
我还在config/environments/production.rb
中明确设置了config.action_cable.url = 'wss://example.com/cable'
和config.action_cable.allowed_request_origins = [ 'https://example.com' ]
,您没有提及,但这是我让Nginx工作之前的方法,因此不确定是否确实需要。