Rails5操作电缆Nginx 404和502错误 [英] Rails5 Action Cable Nginx 404 and 502 errors

查看:125
本文介绍了Rails5操作电缆Nginx 404和502错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人:). 我知道人们已经面临许多与我有关的问题.我已经尝试了所有方法,但问题尚未解决.我过去3天一直在努力解决此问题,但我无法做到.

Everyone :). I know people have already faced alot of problems related to mine. I have tried all but my issue has not been resolved. I have been working from past 3 days to fix this but I am unable to do it.

我第一次使用ActionCable,并且在开发服务器上运行正常.但是在使用Puma和Nginx的生产环境中,我面临着可怕的问题.

I am using ActionCable for the first time and on development server it is working fine. But in production where I am using Puma and Nginx I am facing terrible issues.

最初,当我没有在nginx配置中进行(位置/电缆)设置时,服务器会给我404握手错误
即WebSocket握手期间发生错误:意外的响应代码:404

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

然后在nginx配置中添加以下位置/电缆配置后,我开始收到502错误的网关错误.

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

注意:我尚未打开任何专门用于ActionCable的端口.我认为这不是必需的.我的服务器上仅打开了端口80.

我需要一些专家来帮助我.我需要快速帮助以解决问题.在此先感谢:)

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

在我的环境/production.rb中有这两行内容

I have these two lines present in my environment/production.rb

config.action_cable.url = "ws://my_linode_domain/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]

这是我的Nginx配置文件

This is my nginx config file

    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选项,但没有用.

推荐答案

我为此项目使用的RAILS和RUBY版本是

RAILS and RUBY version I am using for this project is

排名5.0.7

红宝石2.3.1p112

好.因此,今天我想再次将Action Cable集成到我的项目中,但是又遇到了同样的问题.我应用了上述解决方案,但是没有用.上一次虽然可以工作,但是我对解决方案感到不满意,因为为什么ActionCable为什么可以在本地计算机上的单线程/工作器中工作.

ok. So today I wanted to integrate Action Cable again in my project but faced the same issue again. I applied my above solution but it didn't work. Last time although it worked but I wasn't satisfied with the solution thinking that why would ActionCable work in single thread/worker on local machine.

但是这次我集中精力找出了罪魁祸首.

But this time I focussed and figured out the culprit.

罪魁祸首是NGINX配置

遇到404握手错误时的配置

Configuration when I was facing 404 handshake errors

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"表示.

Here we are pointing it to NGINX which is wrong, it should point to our puma server path which in my configuration is represented by 'puma'.

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

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

So to integrate Action Cable with Rails 5 you need to follow following steps:

  1. 在默认端口上设置Redis.
  2. 根据您的应用程序环境,将这些行添加到environment/staging.rb或environment/production.rb中.

  1. Setup Redis on default port.
  2. Add these lines in environments/staging.rb or environments/production.rb, depending on your application environment.

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

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

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

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

如上所述,最后设置您的NGINX文件.这是我在gist nginx.conf 中完整的NGINX配置.我已将网站名称替换为"example.com",并将项目名称替换为"example".因此,如果您要复制任何内容,请确保将其替换为您的内容,否则将无效,因为路径将被破坏.

Finally setup you NGINX file as explained above. Here is my complete NGINX configuration in gist nginx.conf. I had replaced my site name with 'example.com' and project name with 'example'. So if you are copying anything, just make sure you replace those with yours otherwise nothing will work as paths will be broken.

我希望这能真正缓解将ActionCable推向实时应用程序并为任何人解决此握手错误的麻烦,因为这是非常棘手和技术性的工作,许多文档只是提到将动作电缆指向您的主站点URL和不是在nginx后面运行的puma服务器.

I hope this will really release the pain while pushing ActionCable to live application and resolve this handshake error for anyone as this is very very tricky and technical thing and a lot of docs just mention to point action cable to your main site url and not puma server running behind your nginx.

谢谢.

这篇关于Rails5操作电缆Nginx 404和502错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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