【发布时间】:2017-05-05 11:09:23
【问题描述】:
我的 Node.js 应用程序正在 localhost:8080 上运行
这些是服务器配置文件:
var express = require('express');
var http = require('http');
var app = express();
var WS = require('ws');
var config = require('./config/main');
var Client = require('./client');
// HTTP
var server = http.createServer(app);
app.use(express.static(__dirname + '/../client/build/'));
server.listen(config.httpPort, function() {
console.info('HTTP listening on *:' + config.httpPort);
});
// WebSocket
var ws = new WS.Server({server: server});
console.info('Websocket server created');
ws.on('connection', function(ws) {
var client = new Client(ws);
console.log('New conection:', client.id);
});
和
module.exports = {
/* HTTP PORT */
httpPort: process.env.PORT || 8080,
/* WebSocket PORT */
wsPort: process.env.PORT || 8081
};
所以我尝试使用 Nginx 的反向代理运行它:
location /myapp {
proxy_pass http://localhost:8080/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
现在,当我链接到localhost/myapp 时,页面正常显示,加载了所有静态文件,但似乎没有 WebSocket 连接。 PS:我使用的是 Nginx V 1.11.7
配置有问题还是我遗漏了什么?谢谢
【问题讨论】:
-
您是否将 HTTP 版本设置为 1.1?配置语法如下:proxy_http_version 1.1
-
嘿@AlessandroAlinone,是的,我做到了,但它没有改变任何东西
标签: node.js nginx websocket port reverse-proxy