node.js - "Cannot GET"从 Nginx 反向代理到 express.js 上的 socket.io

标签 node.js express nginx socket.io

我关注了this tutorial让 Node.js 通过私有(private)网络在两台 Ubuntu 14.04 服务器上通过 Nginx 工作(Node.js 在 myappserver 上 - 可通过私有(private) IP myprivatewebserver 访问,并通过 mypublicappserver 公开访问 - 并且 Nginx 在 mywebserver 上)。到目前为止一切正常 - 我可以通过 Nginx 访问 myprivateappserver:3000 上的 node.js 应用程序,方法是转到 http://mywebserver/node .

但是,当我尝试运行 chat application在 express.js 上的 socket.io 中,当我直接访问它时它可以工作( http://mypublicappserver:3000 )但是,当我尝试通过我的 Nginx 代理( http://mywebserver/node )访问它时,我在浏览器中得到“Cannot GET/node”并在 Firebug 控制台中“ “网络错误:未找到 404 - http://mywebserver/node”。

如果我 curl http://myprivateappserver:3000从 mywebserver,我可以从我的 socket.io 应用程序中获得 index.html。

我的/etc/nginx/sites-available/default 包含:

location /node{
proxy_pass http://myprivateappserver:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

我的 index.js 是:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

而我的 index.html 是:

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>

这是我第一次涉足 nginx/node/express/socket(我通常是 Apache/CakePHP 人员),所以我很可能遗漏了一些非常简单的东西,但也非常感谢任何有关如何调试的指针出错了

最佳答案

这是因为 SocketIO 默认使用 /socket.io 路径,所以需要配置 Nginx 使其不仅代理 /node 请求,还代理 >/socket.io 也是:

location ~ ^/(node|socket\.io) {
    #your proxy directives
}

(顺便说一句,您似乎有错别字:您编写了 proxypass,但正确的形式是 proxy_pass。其他 proxy 指令)

您还需要在 NodeJS 服务器文件中再执行一次编辑。替换:

app.get('/', function(req, res){

与:

app.get('/node', function(req, res){

现在应该可以了。

这不是唯一的解决方案。您也可以更改 SocketIO path在 Nginx 配置中进行了一些更改,但上面描述的解决方案对我来说似乎是最简单的。祝你好运!

关于node.js - "Cannot GET"从 Nginx 反向代理到 express.js 上的 socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31456871/

相关文章:

javascript - 安装插件后无法在 Jenkins 中看到 Node 选项 'Global Configure'

node.js - 使用 Express 将多个数据库查询结果发送到单个 View

nginx - 将根地址重写为nginx中的子目录

django - nginx 缓存,staticgenerator 与 memcached

azure - 无法访问 AKS 入口的公共(public) IP

node.js - 在 Meteor 中,如何从集合查找游标中获取 Node 读取流?

javascript - 当 DOM 元素出现时如何跳出循环?

node.js - 为什么nodejs HTTP响应不能写汉字?

database - Sequelize : A is not associated with B

javascript - 无法在nodejs中使用toLocaleString