配置AWS Elastic Beanstalk Nginx以使用socket.io连接的主要问题

时间:2016-06-08 09:27:00

标签: node.js amazon-web-services nginx socket.io elastic-beanstalk

我正在AWS Elastic Beanstalk上运行socket.io node.js应用。我没有包括express.js。

显然,Elastic Beanstalk上的Nginx代理在没有手动配置的情况下无法通过TCP连接。

我正在尝试通过我的Android客户端或浏览器连接它。

我已尝试过互联网上的每个解决方案,但我得到502 Bad gateway或超时错误。

我是否应该将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

1 个答案:

答案 0 :(得分:1)

结束必须使用Express.js使用express generator,然后在这里使用答案502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk