Rails Actioncable Nginx 404 和 502 错误

Rails5 Action Cable Nginx 404 and 502 errors

大家:)。 我知道人们已经面临很多与我相关的问题。我已经尝试了所有但我的问题没有得到解决。我过去 3 天一直在努力解决这个问题,但我做不到。

我是第一次使用 ActionCable,在开发服务器上它运行良好。但是在我使用 Puma 和 Nginx 的生产环境中,我遇到了可怕的问题。

Initially when I had not (location /cable) settings in nginx configuration, server gives me 404 handshake error
i.e Error during WebSocket handshake: Unexpected response code: 404

Then after I add following location /cable configuration in nginx configuration I start getting 502 bad gateway error.

Note: I have not opened any port specifically for ActionCable. I assume it is not required. only port 80 is open on my server.

I need some expert to help me with this. I need quick help to get it fixed. Thanks in advance :)

我的 environment/production.rb

中有这两行
config.action_cable.url = "ws://my_linode_domain/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]

这是我的 nginx 配置文件

    upstream app {
      # Path to Puma SOCK file, as defined previously
      server unix:/home/deploy/artcrate/shared/tmp/sockets/puma.sock fail_timeout=0;
    }

    server {
      listen 80;
      #server_name localhost;
      server_name my_linode_domain

     # prevents 502 bad gateway error
     large_client_header_buffers 8 32k;

      root /home/deploy/artcrate/current/public;

      try_files $uri/index.html $uri @app;

      location / {
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Connection '';
        proxy_pass http://app;
      }
      location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    location /cable{
       proxy_pass http://app;
       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;
    }

我在位置/电缆设置中尝试了各种 proxy_pass 选项,但 none 有效。

各位。 :)

经过一个星期的奋斗,努力,不断的摆弄nginx和puma的配置文件,一次又一次的敏锐地阅读博客,我终于找到了问题所在。

我的 nginx 配置是正确的。我不得不在 puma.rb 配置中再添加两行,默认配置中没有这些行。这两行是:

workers 2
daemonize true

daemonize true: 这告诉 puma 在后台 运行 通过生成一个子进程并将其从正在执行的 shell 中分离出来。如果你不使用 daemonize,你需要通过 nohup 运行 puma 进程并显式地把它放在后台。

我不确定我是否需要 workers 2 但我在解决我的问题时添加了它们。所以我让它在那里。但在添加以上两行后,我的 ActionCable 开始正常工作了。

RAILS 和 RUBY 我在这个项目中使用的版本是

Rails 5.0.7

ruby 2.3.1p112

好的。所以今天我想在我的项目中再次集成 Action Cable 但又遇到了同样的问题。我应用了上述解决方案,但没有用。上次虽然它有效,但我对解决方案不满意,认为为什么 ActionCable 在本地机器上以单个 thread/worker 工作。

但这一次我集中精力找出了罪魁祸首。

罪魁祸首是 NGINX 配置

我遇到404握手错误时的配置

location /cable {
proxy_pass http://example.com;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection Upgrade;

}

一切正常时的配置。

location /cable {
proxy_pass http://puma;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection Upgrade;

}

所以罪魁祸首是:

proxy_pass http://example.com;

这里我们将它指向错误的 NGINX,它应该指向我们的 puma 服务器路径,在我的配置中由 'puma'.

表示

这是我在生产服务器上实施 ActionCable 及其工作副本的摘要

因此,要将 Action Cable 与 Rails 5 集成,您需要执行以下步骤:

  1. 在默认端口上安装 Redis。
  2. 在environments/staging.rb 或environments/production.rb 中添加这些行,具体取决于您的应用程序环境。

    config.action_cable.url = [/ws://*/, /wss://*/]

    config.action_cable.allowed_request_origins = [/http://*/, /https://*/]

  3. 最后按照上面的说明设置您的 NGINX 文件。这是我在 gist nginx.conf 中的完整 NGINX 配置。我用 'example.com' 替换了我的站点名称,用 'example' 替换了项目名称。因此,如果您要复制任何内容,请确保将其替换为您的内容,否则将无法正常工作,因为路径会被破坏。

我希望这能在将 ActionCable 推送到实时应用程序时真正减轻痛苦,并为任何人解决此握手错误,因为这是非常棘手和技术性的事情,很多文档只是提到将 action cable 指向您的主站点url 而不是你的 nginx 后面的 puma 服务器 运行。

谢谢。