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

"Cannot GET" on reverse proxy from Nginx to socket.io on express.js

我关注了this tutorial to get Node.js working through Nginx on a two Ubuntu 14.04 servers via private networking (Node.js is on myappserver - accessible via private IP myprivatewebserver and publicly via mypublicappserver - and Nginx on mywebserver). Everything works fine til this point - I can access a node.js application on myprivateappserver:3000 proxied via Nginx by going to http://mywebserver/node

但是,当我尝试 运行 向上 chat application in socket.io on express.js, it works when I access it directly (http://mypublicappserver:3000) but, when I try and access it via my Nginx proxy (http://mywebserver/node) 时,我在浏览器和 firebug 控制台中得到 "Cannot GET /node"“ "NetworkError: 404 Not Found - http://mywebserver/node"。

如果我从我的网络服务器 curl http://myprivateappserver:3000,我可以从我的 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){

现在应该可以了。

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

Curious 的回答很好。我想补充一点,如果您使用 sub_filter,则不必调整 node.js 应用程序。你的 nginx 一定是用那个模块编译的。

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

location ~ /(node|socket.io)  {
.......

}

这也适用于没有“^”和“\”符号的情况。

我觉得这样会更好:

location /node{
proxy_pass http://myprivateappserver:3000/; # add "/"
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;
}

proxy_pass加'/',然后app.get('/')也可以!