4

我正在將Heroku中的node.js應用程序遷移到使用端口80上的WebSockets的AWS Elastic Beanstalk。WebSockets在AWS Elastic Beanstalk上返回301錯誤,但在Heroku上沒有返回301錯誤。使用端口80的AWS Elastic Beanstalk上的WebSocket問題

要初始化WebSocket連接,請單擊Create a new note

AWS青苗 http://default-environment.psgekxtsbd.us-west-2.elasticbeanstalk.com/

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

這是我如何設置在服務器上的WebSockets。

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); 

我試圖在.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"; 

當我添加這些配置的HTML失敗,錯誤ERR_NAME_NOT_RESOLVED加載。這是網址http://websocketsissue.us-west-2.elasticbeanstalk.com/

我使用Node.js的預定義配置將我的彈性beanstalk環境設置爲Web服務器環境。我沒有添加任何額外的資源。

有沒有辦法讓WebSockets在使用AWS Elastic Beanstalk的端口80上工作?

更新

我能得到的單一實例的環境中工作,而不是負載平衡,自動縮放的環境。

爲了讓單個實例工作,我增加了以下文件.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,並將文件服務器和REST服務保留在端口80上。它給我一個EB錯誤502,但它在我的本地主機上工作 – sissonb

回答

4

我在這裏找到了部分答案,https://scopestar.com/blog/aws/elasticbeanstalk/websockets/2016/10/21/enable-websockets-on-elasticbeanstalk-nginx-proxy.html

我在.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 

爲了讓WebSockets向負載平衡的工作,自動縮放的環境,我需要做的除了

  • 轉到下面的EC2儀表板
  • 找到負載平衡器彈性創建Beanstalk項目
  • 右鍵單擊負載均衡器,然後單擊編輯偵聽器
  • 將負載平衡器協議從HTTP更改爲TCP端口80並保存更改es
+0

你如何確定哪個負載均衡器屬於eb項目? –

+0

@ToriHuang在爲要部署的EB創建環境時,它將記錄創建的負載平衡器的名稱。您可以查看EB頁面上的事件日誌並查找名稱。它應該從awseb開始。 – sissonb