如何在生产中使用Nginx和Unicorn配置ActionCable? [英] How to configure ActionCable with Nginx and Unicorn in production?

查看:100
本文介绍了如何在生产中使用Nginx和Unicorn配置ActionCable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近已将Rails项目从Rails4切换到5.0.0.beta3,以使用超赞的ActionCable.

I've recently switched my rails project from Rails4 to 5.0.0.beta3 to use the awesome ActionCable.

我的ActionCable服务器在独角兽内部运行.在开发中一切正常.在生产中我有

My ActionCable server is run inside unicorn. In development all works fine. In production I have

Started GET "/cable" for xxx.xxx.xxx.xxx at 2016-03-28 18:06:38 +0300
Started GET "/cable/" [WebSocket] for xxx.xxx.xxx.xxx at 2016-03-28 18:06
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Registered connection (189772ff-6229-48f1-ae7f-d9a96ad3a6c3)
Finished "/cable/" [WebSocket] for xxx.xxx.xxx.xxx at 2016-03-28 18:06:35

此消息一次又一次地循环.

And this message repeats again and again in a loop.

我在stackoverflow上尝试了很多选项来解决此问题,但没有任何帮助. 我的nginx配置:

I've tried a lot of options at stackoverflow to handle this but nothing helps. My nginx config:

 upstream unicorn {
   server unix:/tmp/unicorn.my_app.sock fail_timeout=0;
 }

 server {
   server_name www.my_app.com;
   return 301 $scheme://my_app.com$request_uri;
 }

 server {
   listen 80 default deferred;
   server_name my_app.com;
   root /var/www/my_app/current/public;

   location ^~ /assets/ {
     gzip_static on;
     expires max;
     add_header Cache-Control public;
   }

   try_files $uri/index.html $uri @unicorn;
   location @unicorn {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_redirect off;
     proxy_pass http://unicorn;
   }

   location /cable {
     proxy_pass http://unicorn/cable;
     proxy_http_version 1.1;
     proxy_set_header Upgrade websocket;
     proxy_set_header Connection Upgrade;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }

   error_page 500 502 503 504 /500.html;
   keepalive_timeout 5;
 }

为确保允许请求,我已在初始化程序中临时使用此代码: ActionCable.server.config.disable_request_forgery_protection = true

To be sure that request is allowed I've temporarily use this code in initializers: ActionCable.server.config.disable_request_forgery_protection = true

我的cable.coffee文件

 @App ||= {}
 App.cable = ActionCable.createConsumer "/cable"

我的config/cable.yml文件

production:
 adapter: redis
 url: redis://localhost:6379/1

我在这个问题上经验不足,所以任何帮助都会很棒.

I'm not so experienced at this question, so any help would be great.

推荐答案

location /cable部分中需要添加一行proxy_set_header Host $http_host;

In the location /cable section need to add a line proxy_set_header Host $http_host;

应该是:

location /cable {
     proxy_pass http://unicorn/cable;
     proxy_http_version 1.1;
     proxy_set_header Upgrade websocket;
     proxy_set_header Connection Upgrade;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
   }

这篇关于如何在生产中使用Nginx和Unicorn配置ActionCable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆