Nginx + Node.js + Socket.io + SSL 可以吗?
Is Nginx + Node.js + Socket.io + SSL possible?
我正在尝试 运行 一个 socket.io 以 nginx 作为代理的聊天应用程序。当我通过 http+port 连接到服务器时它工作正常,但它不适用于 https。我看到用户 connected/disconnected 事件通过,但没有发出到达客户端或服务器。
这是我的服务器 .conf (nginx/1.4.6 Ubuntu)
upstream websocket {
server 127.0.0.1:8090;
}
server {
listen 80;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /home/andrew/example.com/nginx/certs/example.com.cer;
ssl_certificate_key /home/andrew/example.com/nginx/certs/example.com.private.key;
root /home/andrew/example.com/public;
index index.html index.htm;
server_name example.com;
location /chat/ {
rewrite ^/chat/?(.*)$ / break;
proxy_pass http://websocket;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location / {
try_files $uri $uri/ =404;
}
}
服务器(节点 v0.12.1,Socket.io v1.3.5)
var express = require('express');
var app = express();
var server = require('http').createServer( app );
var io = require("socket.io").listen( server );
server.listen(8090);
console.log('listening on port 8090');
io.sockets.on( 'connection', function( socket ){
console.log( 'user connected' );
var msg = "user connected!!!";
socket.emit( 'message', msg );
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('message', function( msg ){
socket.emit( 'message', msg );
console.log('message: ' + msg);
});
});
客户
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://example.com/chat/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io( "https://example.com/chat", {path: '/chat/socket.io'} );
$('form').submit(function(){
socket.emit('message', $('#m').val());
$('#m').val('');
return false;
});
socket.emit('message', "sup");
socket.on('message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
好的,原来这是 node.js 代码中 socket.io 的命名空间的问题。更多信息在这里:http://socket.io/docs/rooms-and-namespaces
这是服务器的一个工作示例
var app = require( 'express' )();
var http = require( 'http' ).Server( app );
var io = require( 'socket.io' )( http );
var nsp = io.of('/chat'); // this is what needs to happen
// and then we're listening to communication in the proper namespace
nsp.on( 'connection', function( socket ){
console.log( 'user connected' );
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('message', function(msg){
nsp.emit( 'message', msg ); // this will broadcast message to everybody connected within the same namespace
console.log('message: ' + msg);
});
});
http.listen( 8090, function(){
console.log( "listening on :8090" );
});
我正在尝试 运行 一个 socket.io 以 nginx 作为代理的聊天应用程序。当我通过 http+port 连接到服务器时它工作正常,但它不适用于 https。我看到用户 connected/disconnected 事件通过,但没有发出到达客户端或服务器。
这是我的服务器 .conf (nginx/1.4.6 Ubuntu)
upstream websocket {
server 127.0.0.1:8090;
}
server {
listen 80;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /home/andrew/example.com/nginx/certs/example.com.cer;
ssl_certificate_key /home/andrew/example.com/nginx/certs/example.com.private.key;
root /home/andrew/example.com/public;
index index.html index.htm;
server_name example.com;
location /chat/ {
rewrite ^/chat/?(.*)$ / break;
proxy_pass http://websocket;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location / {
try_files $uri $uri/ =404;
}
}
服务器(节点 v0.12.1,Socket.io v1.3.5)
var express = require('express');
var app = express();
var server = require('http').createServer( app );
var io = require("socket.io").listen( server );
server.listen(8090);
console.log('listening on port 8090');
io.sockets.on( 'connection', function( socket ){
console.log( 'user connected' );
var msg = "user connected!!!";
socket.emit( 'message', msg );
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('message', function( msg ){
socket.emit( 'message', msg );
console.log('message: ' + msg);
});
});
客户
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://example.com/chat/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io( "https://example.com/chat", {path: '/chat/socket.io'} );
$('form').submit(function(){
socket.emit('message', $('#m').val());
$('#m').val('');
return false;
});
socket.emit('message', "sup");
socket.on('message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
好的,原来这是 node.js 代码中 socket.io 的命名空间的问题。更多信息在这里:http://socket.io/docs/rooms-and-namespaces
这是服务器的一个工作示例
var app = require( 'express' )();
var http = require( 'http' ).Server( app );
var io = require( 'socket.io' )( http );
var nsp = io.of('/chat'); // this is what needs to happen
// and then we're listening to communication in the proper namespace
nsp.on( 'connection', function( socket ){
console.log( 'user connected' );
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('message', function(msg){
nsp.emit( 'message', msg ); // this will broadcast message to everybody connected within the same namespace
console.log('message: ' + msg);
});
});
http.listen( 8090, function(){
console.log( "listening on :8090" );
});