我在Ubuntu14.04LTS机器上有一个简单的nodejs。我如何配置nginx或乘客侦听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));
});
});
/* ======================================== */在我的静态/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启用/默认文件:
#===================#
#==== 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,并通过websockets ://localhost: 8443 /helloworld连接到运行在8443上的node.js服务器。
但是当我运行sudo service nginx start时,我不能再用ws://localhost: 8443 /helloworld在8443上运行的node.js服务器交换websockets了。
我如何配置nginx或乘客侦听ws://localhost:8443/helloworld的呼叫,即使客户机位于localhost:3000上?
发布于 2017-11-29 20:36:51
你不能让你的应用程序和nginx在同一个端口上监听。您将需要您的应用程序来侦听另一个端口上的websocket连接,并将nginx从8443转发到该端口。
另外,如果您在应用程序中使用多个端口,则需要告诉乘客哪个是“主”端口:binding.html#caveat-multiple-http-server-objects-error-http-server-listen-was-called-more-than-once。
https://stackoverflow.com/questions/47492126
复制相似问题