ruby-on-rails - Rails Actioncable Nginx 404 和 502 错误

标签 ruby-on-rails nginx ruby-on-rails-5 puma actioncable

每个人 :)。 我知道人们已经面临很多与我相关的问题。我已经尝试了所有但我的问题没有得到解决。过去 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 :)

我的环境/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;
    }

我在 location/cable 设置中尝试了各种 proxy_pass 选项,但都没有用。

最佳答案

每个人。 :)

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

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

workers 2
daemonize true

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

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

关于ruby-on-rails - Rails Actioncable Nginx 404 和 502 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43772674/

相关文章:

javascript - 用户无法在 js.erb 文件中渲染

ruby-on-rails - 在迁移期间使用模型方法填充列

bash - 如何在 gitlab ci 的构建脚本中使用 sudo?

Rails Helper 中的 Javascript、Jquery

jquery - GON - 在 js.erb + Rails 5 中没有获得值(value)

sql - 显示消息数组中的最后一条消息

ruby-on-rails - Rails 5 API;更新单个记录

nginx - 从Kubernetes NodePort服务进行负载平衡/重定向

nginx - 命名 Docker 卷以共享构建而不更新

ruby-on-rails - 使用符号访问对象属性