Coder Social home page Coder Social logo

Comments (4)

jcsky avatar jcsky commented on June 12, 2024

目前確定問題在於 server 前掛了層 cloudflare 及 aws ELB,導致 websocket connect failed

繞過 cloudflare,直接連到server就正常了
Rails.application.config.action_cable.allowed_request_origins = 'http://ip-address'
chrome url 打 ip-address 而不是 domain_url

from ama.

jcsky avatar jcsky commented on June 12, 2024

因為 cloudflare 寫 support websocket in any plan
估且先相信它了
試了以下幾個做法

想讓 ELB support websocket
照以下的教學在 ELB 多加了個 listener,並用 aws-cli 設定好了
https://blog.jverkamp.com/2015/07/20/configuring-websockets-behind-an-aws-elb/
但沒有效

想讓websocket直接跳過 ELB 跟 server 溝通,在 config/environments/production.rb 中加了

Rails.application.config.action_cable.allowed_request_origins = [ 'https://socket.amazing-tutor.com', /https:\/\/socket.amazing-tutor.*/ ]
config.action_cable.url = "wss://socket.amazing-tutor.com/cable"

layout 中加了 <%= action_cable_meta_tag %>
並在 nginx conf 的 server name 加了 socket.amazing-tutor.com

然後將 socket.amazing-tutor.com 的 DNS 指向 server ip,而非 ELB ip
但仍然是錯誤的

WebSocket connection to 'wss://socket.amazing-tutor.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 503
WebSocket connection to 'wss://socket.amazing-tutor.com/cable' failed: WebSocket is closed before the connection is established.

想將 config.action_cable.url = "ws://server-ip/cable"
讓 websocket 直接與 server 溝通
但因為網頁是 https:// ,所以不能使用 ws,必須要有加密的 wss
目前 https 是 cloudflare 提供,所以似乎不能不經過 cloudflare

from ama.

jcsky avatar jcsky commented on June 12, 2024

目前成功的做法是,DNS 直接指向 server-ip,不論 http, https, websocket 都不經過ELB
不過 cloudflare 仍然保留

from ama.

imgarylai avatar imgarylai commented on June 12, 2024

我有遇到和你類似的問題,但因為架構跟你的有些不同,所以解決的方式也有些不同,但或許可以參考一下

我是透過 elastic beanstalk 部署我的 Rails Application ,並且使用 Nginx + Puma 而不是 Phusion

SSL 的設定我是在 load balancer 上,其中 80, 443 的 port 原本是使用 http, https 作為通訊協定,要分別改成 tpc, ssl 作為通訊協定

nginx 的設定檔

      client_max_body_size 1G;
      server_names_hash_bucket_size 128;

      upstream backend {
        server unix:///var/run/puma/my_app.sock;
      }

      server {
        listen 80;
        listen 443 ssl;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        server_name YOUR_SERVER_NAME;

        large_client_header_buffers 8 32k;

        location / {
          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;
          proxy_set_header X-NginX-Proxy true;
          proxy_set_header X-Forwarded-Proto https;

          proxy_buffers 8 32k;
          proxy_buffer_size 64k;

          proxy_pass http://backend;
          proxy_redirect off;

          location /assets {
            root /var/app/current/public;
          }

          # enables WS support
          location /cable {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto https;
            proxy_redirect off;
          }
        }
      }

from ama.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.