NGINX 监听 websocket 客户端的调用
NGINX listen websocket client's call
我在 Ubuntu 14.04 LTS 机器上有一个简单的 nodejs。
如何配置 nginx 或 passenger 以监听对 ws://localhost:8443/helloworld 的调用,即使客户端在 localhost:3000?
server.js 看起来像:
var path = require('path');
var ws = require('ws');
var express = require('express');
var minimist = require('minimist');
var url = require('url');
var fs = require('fs');
var http = require('http');
var app = express();
app.use(express.static(path.join(__dirname, 'static')));
/* ======================================== */
/* ======= ======== */
/* ======= WEBSOCKETS SERVER ======== */
/* ======= ======== */
/* ======================================== */
var asUrl = url.parse("http://localhost:8443/");
var port = asUrl.port;
var server = http.createServer(app).listen(port, function() {
console.log('********** WS HTTP SERVER STARTED********** ');
});
var wss = new ws.Server({
server : server,
path : '/helloworld'
});
/* ======================================== */
/* ========= WSS ON CONNETION ======== */
/* ======================================== */
wss.on('connection', function(ws) {
console.log('**** CONNECTION RECEIVED =D');
ws.send(JSON.stringify("WELCOME FROM SERVER =D"));
ws.on('message', function incoming(data) {
console.log('********** RECEIVED MSG '+data);
ws.send(JSON.stringify(data));
});
});
/* ======================================== */
在我的 static/index.html 文件中我有:
<!-- ======================================= -->
<!-- ========== CSS =========== -->
<!-- ======================================= -->
<style>
p { line-height:18px; }
div { width:500px; margin-left:auto; margin-right:auto;}
#content { padding:5px; background:#ddd; border-radius:5px;
overflow-y: scroll; border:1px solid #CCC;margin-top:10px; height: 160px; }
#input_hello { border-radius:2px; border:1px solid #ccc; margin:10px; padding:5px; width:50%; float:left;}
#status { width:88px;display:block;float:left;margin-top:15px; }
</style>
<!-- ======================================= -->
<!-- ========== JS FOR WEBSOCKET =========== -->
<!-- ======================================= -->
<script type="text/javascript">
$(document).ready(function () {
//-- ======================================= --//
// ======================== //
// ====== CONNECTION ====== //
// ======================== //
var content = $('#content');
var message = $('#input_hello input[type="text"]').val();
var ws = new WebSocket('ws://localhost:8443/helloworld');
ws.onopen = function() {
alert("YOU ARE CONNECTED WITH WSS!!!!");
};
// ======================== //
// ====== ON KEY PRESS ==== //
// ======================== //
$("#input_hello").keydown(function(event) {
// ======= IF IS ENTER ===== //
if (event.keyCode == 13) {
ws.send(message);
return false;
}
// ======================== //
});
// ======================== //
// ====== WS MESSAGE ====== //
// ======================== //
ws.onmessage = function(msg) {
alert("RECEIVED MESSAGE WITH WSS!!!!");
addMessage(msg.data);
}
// ======================== //
// ==== RENDER MESSAGE ==== //
// ======================== //
function addMessage(message) {
content.prepend('<p><span>' + message + '</span></p>');
$("#input_hello").val("");
}
//-- ======================================= --//
});
</script>
<!-- ======================================== -->
<!-- ======================================== -->
<!-- ======== HTML ======= -->
<!-- ======================================== -->
<!-- ======================================== -->
<h1>HELLO FROM EXPRESS</h1>
<div id="content"></div>
<div>
<input type="text" id="input_hello" />
</div>
<!-- ======================================== -->
这是我的etc/nginx/sites-enabled/default文件:
#===================#
#==== SERVER::80 ===#
#===================#
server {
listen 80 default_server;
listen [::]:80 default_server;
#========================#
#==== FOR ROOT =====#
#========================#
root /home/deploy/Desktop/NODE_APP/static;
#========================#
#==== FOR PASSENGER =====#
#========================#
passenger_enabled on;
passenger_app_env development;
passenger_app_type node;
passenger_app_root /home/deploy/Desktop/NODE_APP;
passenger_startup_file /home/deploy/Desktop/NODE_APP/server.js;
location / {
try_files $uri $uri/ =404;
}
}
#===================#
#=== SERVER 8443 ===#
#===================#
server {
listen 8443;
listen [::]:8443;
#========================#
#==== FOR ROOT =====#
#========================#
root /nowhere; #========== NO NEED STATIC FILES..(ONLY WS!)
#========================#
#==== FOR WEBSOCKETS ====#
#========================#
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://localhost:8443/helloworld;
}
}
#=======================================#
#=======================================#
当我 运行 node /home/deploy/Desktop/NODE_APP/server.js
时,我可以访问 localhost:3000 或 localhost:8443 并连接到 8443 上的 node.js 服务器 运行ning通过 ws://localhost:8443/helloworld.
上的 websockets
但是当我 运行 sudo service nginx start
时,我无法再通过 ws://[=39= 在 8443 上与 node.js 服务器 运行ning 交换 websockets ]/你好世界
如何配置 nginx 或 passenger 来监听对
ws://localhost:8443/helloworld,即使客户端在 localhost:3000?
你不能让你的应用程序和 nginx 在同一个端口上侦听。您需要您的应用程序在另一个端口上侦听 websocket 连接,并让 nginx 从 8443 转发到该端口。
此外,如果您在应用中使用多个端口,则需要告诉 Passenger 哪个是 "main":https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html#caveat-multiple-http-server-objects-error-http-server-listen-was-called-more-than-once
我在 Ubuntu 14.04 LTS 机器上有一个简单的 nodejs。 如何配置 nginx 或 passenger 以监听对 ws://localhost:8443/helloworld 的调用,即使客户端在 localhost:3000?
server.js 看起来像:
var path = require('path');
var ws = require('ws');
var express = require('express');
var minimist = require('minimist');
var url = require('url');
var fs = require('fs');
var http = require('http');
var app = express();
app.use(express.static(path.join(__dirname, 'static')));
/* ======================================== */
/* ======= ======== */
/* ======= WEBSOCKETS SERVER ======== */
/* ======= ======== */
/* ======================================== */
var asUrl = url.parse("http://localhost:8443/");
var port = asUrl.port;
var server = http.createServer(app).listen(port, function() {
console.log('********** WS HTTP SERVER STARTED********** ');
});
var wss = new ws.Server({
server : server,
path : '/helloworld'
});
/* ======================================== */
/* ========= WSS ON CONNETION ======== */
/* ======================================== */
wss.on('connection', function(ws) {
console.log('**** CONNECTION RECEIVED =D');
ws.send(JSON.stringify("WELCOME FROM SERVER =D"));
ws.on('message', function incoming(data) {
console.log('********** RECEIVED MSG '+data);
ws.send(JSON.stringify(data));
});
});
/* ======================================== */
在我的 static/index.html 文件中我有:
<!-- ======================================= -->
<!-- ========== CSS =========== -->
<!-- ======================================= -->
<style>
p { line-height:18px; }
div { width:500px; margin-left:auto; margin-right:auto;}
#content { padding:5px; background:#ddd; border-radius:5px;
overflow-y: scroll; border:1px solid #CCC;margin-top:10px; height: 160px; }
#input_hello { border-radius:2px; border:1px solid #ccc; margin:10px; padding:5px; width:50%; float:left;}
#status { width:88px;display:block;float:left;margin-top:15px; }
</style>
<!-- ======================================= -->
<!-- ========== JS FOR WEBSOCKET =========== -->
<!-- ======================================= -->
<script type="text/javascript">
$(document).ready(function () {
//-- ======================================= --//
// ======================== //
// ====== CONNECTION ====== //
// ======================== //
var content = $('#content');
var message = $('#input_hello input[type="text"]').val();
var ws = new WebSocket('ws://localhost:8443/helloworld');
ws.onopen = function() {
alert("YOU ARE CONNECTED WITH WSS!!!!");
};
// ======================== //
// ====== ON KEY PRESS ==== //
// ======================== //
$("#input_hello").keydown(function(event) {
// ======= IF IS ENTER ===== //
if (event.keyCode == 13) {
ws.send(message);
return false;
}
// ======================== //
});
// ======================== //
// ====== WS MESSAGE ====== //
// ======================== //
ws.onmessage = function(msg) {
alert("RECEIVED MESSAGE WITH WSS!!!!");
addMessage(msg.data);
}
// ======================== //
// ==== RENDER MESSAGE ==== //
// ======================== //
function addMessage(message) {
content.prepend('<p><span>' + message + '</span></p>');
$("#input_hello").val("");
}
//-- ======================================= --//
});
</script>
<!-- ======================================== -->
<!-- ======================================== -->
<!-- ======== HTML ======= -->
<!-- ======================================== -->
<!-- ======================================== -->
<h1>HELLO FROM EXPRESS</h1>
<div id="content"></div>
<div>
<input type="text" id="input_hello" />
</div>
<!-- ======================================== -->
这是我的etc/nginx/sites-enabled/default文件:
#===================#
#==== SERVER::80 ===#
#===================#
server {
listen 80 default_server;
listen [::]:80 default_server;
#========================#
#==== FOR ROOT =====#
#========================#
root /home/deploy/Desktop/NODE_APP/static;
#========================#
#==== FOR PASSENGER =====#
#========================#
passenger_enabled on;
passenger_app_env development;
passenger_app_type node;
passenger_app_root /home/deploy/Desktop/NODE_APP;
passenger_startup_file /home/deploy/Desktop/NODE_APP/server.js;
location / {
try_files $uri $uri/ =404;
}
}
#===================#
#=== SERVER 8443 ===#
#===================#
server {
listen 8443;
listen [::]:8443;
#========================#
#==== FOR ROOT =====#
#========================#
root /nowhere; #========== NO NEED STATIC FILES..(ONLY WS!)
#========================#
#==== FOR WEBSOCKETS ====#
#========================#
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://localhost:8443/helloworld;
}
}
#=======================================#
#=======================================#
当我 运行 node /home/deploy/Desktop/NODE_APP/server.js
时,我可以访问 localhost:3000 或 localhost:8443 并连接到 8443 上的 node.js 服务器 运行ning通过 ws://localhost:8443/helloworld.
但是当我 运行 sudo service nginx start
时,我无法再通过 ws://[=39= 在 8443 上与 node.js 服务器 运行ning 交换 websockets ]/你好世界
如何配置 nginx 或 passenger 来监听对 ws://localhost:8443/helloworld,即使客户端在 localhost:3000?
你不能让你的应用程序和 nginx 在同一个端口上侦听。您需要您的应用程序在另一个端口上侦听 websocket 连接,并让 nginx 从 8443 转发到该端口。
此外,如果您在应用中使用多个端口,则需要告诉 Passenger 哪个是 "main":https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html#caveat-multiple-http-server-objects-error-http-server-listen-was-called-more-than-once