1

我正在AWS Elastic Beanstalk上運行socket.io node.js應用程序。我沒有包含express.js。配置AWS Elastic Beanstalk Nginx使用socket.io連接的主要問題

Elastic Beanstalk上的Nginx代理顯然不通過TCP連接,而無需手動配置。

我試圖通過我的Android客戶端或瀏覽器連接到它。

我已經嘗試從字面上每個解決方案在互聯網上,但我要麼502錯誤的網關,或超時錯誤。

我應該將AWS Elastic Beanstalk負載平衡器更改爲接受TCP非HTTP嗎?

我的node.js應用程序正在監聽端口3000(硬編碼)。在localhost上運行良好。

var server = require('http').createServer(handleRequest); 
var io = require('socket.io')(server); 

function handleRequest(request, response) 
{ 
    response.end('It Works!! Path Hit: ' + request.url); 
} 

io.on('connection', function (socket) 
{ 
    console.log('a user connected'); 

    socket.on('disconnect', function() 
    { 
     console.log('user disconnected'); 
    }); 

    socket.on('chat message', function (msg) 
    { 
     console.log('message received on server: ' + msg); 

     io.emit('chat message', msg); 
    }); 
}); 

server.listen(3000, function() 
{ 
    console.log("Server listening on port:", 3000); //Callback triggered when server is successfully listening. Hurray! 
}); 

這裏是我的package.json

{ 
    "name": "qurba-messaging-backend", 
    "version": "0.0.1", 
    "scripts": { 
    "start": "node main.js" 
    }, 
    "dependencies": { 
    "socket.io": "^1.4.6" 
    } 
} 

這裏是我的nginx的配置https://gist.github.com/RobinBuschmann/c42a413ecedee463e5bd8e613ddd8d04

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

     server { 
     listen 80; 

     location/{ 
      proxy_pass http://nodejs; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection "upgrade"; 
      proxy_http_version 1.1; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     } 
     } 
    "/opt/elasticbeanstalk/hooks/appdeploy/enact/41_remove_eb_nginx_confg.sh": 
    mode: "000755" 
    owner: root 
    group: root 
    content : | 
     mv /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf.old 

回答