我正在將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
它可以與其他端口一起使用嗎?我知道8080是你嘗試過的默認配置,或者你需要它是80 – Lyon
我剛剛嘗試將ws服務移動到端口8080,並將文件服務器和REST服務保留在端口80上。它給我一個EB錯誤502,但它在我的本地主機上工作 – sissonb