简体   繁体   English

Rails + ActionCable + Nginx + Unicorn设置

[英]Rails + ActionCable + Nginx + Unicorn Setup

I've been trying to deploy a Rails 5 + ActionCable app in Ubuntu using Nginx + Unicorn. 我一直在尝试使用Nginx + Unicorn在Ubuntu中部署Rails 5 + ActionCable应用程序。

Normal http request work just fine, but Websockets connection are rejected, I suppose is an problem with my Nginx. 正常的http请求工作正常,但Websockets连接被拒绝,我想我的Nginx是个问题。

I been wondering in Google, found similar questions and solutions but nothing seem to work: 我一直想知道谷歌,发现类似的问题和解决方案,但似乎没有任何作用:

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

- NGINX configuration for Rails 5 ActionCable with puma - 使用puma的Rails 5 ActionCable的NGINX配置

- Rails 5 Action Cable deployment with Nginx, Puma & Redis - 使用Nginx,Puma和Redis部署Rails 5 Action Cable

I even try adapting NodeJS setup for nginx and websockets https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/ 我甚至尝试为nginx和websockets调整NodeJS设置https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/

The error I get is 'ws://mydomain.com/cable' failed: Error during WebSocket handshake: 404 我得到的错误是'ws://mydomain.com/cable'失败:WebSocket握手期间出错:404

upstream app_server {
   server unix:/var/run/unicorn.sock fail_timeout=0;
}

server {
   listen   80;
   root /home/rails/rails_project/public;
   server_name _;
   index index.htm index.html;

   location / {
        try_files $uri/index.html $uri.html $uri @app;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|ta$
                try_files $uri @app;
        }

 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://app_server;
}
location /cable {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://app_server;
}

}

Thanks in advance 提前致谢

Fixed similar error (404 in actioncable /cable) in my setup like this 在我的设置中修复了类似的错误(actioncable / cable中的404)

location /cable {
    proxy_pass http://unix:/home/app/shared/tmp/puma/socket;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Host $host:$server_port;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM