4

ノード80のWebSocketを使用するAWS Elastic Beanstalkに、Herokuからnode.jsアプリケーションを移行しています。WebSocketはAWS Elastic Beanstalkでは301エラーを返していますが、Herokuでは301エラーを返しています。ポート80を使用したAWS Elastic Beanstalkの問題

WebSocket接続を初期化するには、Create a new noteをクリックします。

AWS Beanstalkの http://default-environment.psgekxtsbd.us-west-2.elasticbeanstalk.com/

Herokuの https://murmuring-shelf-40601.herokuapp.com/

これは、私は、サーバー上のWebSocketを設定しています方法です。

const express = require('express'); 
const app = express(); 
require("express-ws")(app); 
app.post("/service/create-account/", (req, res)=> {/*code in here*/}); 
app.ws('/:noteId/', function (ws, req) {/*code in here*/}); 
const port = process.env.PORT || 3000; 
app.listen(port); 

私は、これらの構成を追加するとHTMLがエラーERR_NAME_NOT_RESOLVEDでロードに失敗した、などの.ebextensionsフォルダ内の異なる構成を追加

files: 
    "/etc/nginx/conf.d/01_websockets.conf" : 
     mode: "000644" 
     owner: root 
     group: root 
     content : | 
      upstream nodejs { 
       server 127.0.0.1:80; 
       keepalive 256; 
      } 

      server { 
       listen 80; 

       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; 

        # prevents 502 bad gateway error 
        proxy_buffers 8 32k; 
        proxy_buffer_size 64k; 

        proxy_pass http://nodejs; 
        proxy_redirect off; 

        # enables WS support 
        proxy_http_version 1.1; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection "upgrade"; 
       } 
      } 

そして

files: 
    "/etc/nginx/conf.d/websocketupgrade.conf" : 
     mode: "000755" 
     owner: root 
     group: root 
     content: | 
      proxy_set_header  Upgrade   $http_upgrade; 
      proxy_set_header  Connection  "upgrade"; 

を試してみました。 URLは以下の通りですhttp://websocketsissue.us-west-2.elasticbeanstalk.com/

Node.jsの定義済み構成を使用して、自分の弾力のあるbeanstalk環境をWebサーバー環境としてセットアップしました。私は追加のリソースを追加しませんでした。

AWS Elastic Beanstalkを使用してポート80でWebSocketを動作させる方法はありますか?

更新

私が動作するように、単一インスタンス環境を得ることができたではなく、ロードバランス、オートスケーリング環境。動作するように単一のインスタンスを取得するには

私は.ebextensions

container_commands: 
    01_nginx_websocket_support: 
    command: | 
     sed -i '/\s*proxy_set_header\s*Connection/c \ 
       proxy_set_header Upgrade $http_upgrade;\ 
       proxy_set_header Connection "upgrade";\ 
      ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf 
+0

他のポートでも機能しますか?私は8080がデフォルト設定であることを知っていますが、それを試してみましたか、それとも80である必要がありますか? – Lyon

+0

wsサービスをポート8080に移動して、ポート80でファイルサーバーとRESTサービスを維持しようとしました。それは私にEBの502エラーを与えますが、それは私のlocalhostで動作します – sissonb

答えて

4

に次のファイルを追加した私は、私は次の内容の.ebextensionsフォルダ内のファイルを追加https://scopestar.com/blog/aws/elasticbeanstalk/websockets/2016/10/21/enable-websockets-on-elasticbeanstalk-nginx-proxy.html

、ここでの回答の一部を発見しましたシングルインスタンス環境用に修正されました。 EC2ダッシュボード

  • にWebSocketをを加え

    に、私は次の操作を実行するために必要な負荷分散、自動スケーリング環境に
    • を動作させるために

      container_commands: 
          01_nginx_websocket_support: 
          command: | 
           sed -i '/\s*proxy_set_header\s*Connection/c \ 
             proxy_set_header Upgrade $http_upgrade;\ 
             proxy_set_header Connection "upgrade";\ 
            ' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf 
      

      ゴーでは、Load Balancer弾性を探しますBeanstalkプロジェクトが作成されました

    • ロードバランサを右クリックしてリスナーを編集
    • ロードバランサプロトコルをポート80のHTTPからTCPに変更して、 es
  • +0

    どのロードバランサがebプロジェクトに属しているかをどのように判断しますか? –

    +0

    @ToriHuang EBを展開する環境を作成すると、作成されたロードバランサの名前がログに記録されます。 EBページのイベントログを見て名前を見つけることができます。それはawsebから始めるべきです。 – sissonb

    関連する問題