我在nginx后面运行的单个服务器上有一个快速应用程序 我已经测试了socket.io通信,它在我们的开发服务器上运行良好。 将ssl添加到混合中会在客户端上生成以下控制台日志: 您可以注意到它产生400(错误请求) - 然后成功加载相同的请求,但我的控制台日志没有运行,因为连接不成功... 非常感谢任何反馈。谢谢。 这是代码:
这是我的nginx配置:
upstream example.com {
server 127.0.0.1:3000;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/nginx/ssl/example.crt;
ssl_certificate_key /path/to/nginx/ssl/example.key;
location / {
proxy_pass http://example.com;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto https;
}
location /public {
root /var/www/example/;
}
location /favicon.ico {
root /var/www/example/public/favicon.ico;
}
}
server {
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
这是我在快递服务器上的socket.io相关代码
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(port, function(){
console.log('MyDomain.com app listening at ', 'http://localhost:'+port);
io.on('connection', function(socket){
console.log('socket.io connected','socket.id: '+socket.id);
socket.on('new-user', function(data) {
var user_id = data.user_id;
console.log('new-user, user_id: ', user_id);
});
});
});
这是我尝试连接的客户端:
var socket = io.connect(null, { secure: true, port: 443, rememberTransport: false, 'reopen delay': 1000 });
//I have also tried these variations...
//-----------------------------------------
//var socket = io();
//var socket = io.connect('https://localhost', {secure: true});
//var socket = io.connect('https://example.com',{secure: true});
//var socket = io.connect('https://example.com');
//-----------------------------------------
socket.on('connect', function () {
console.log('on "connect"');
socket.emit('new-user', {user_id:some_user_id});
});
在我的html模板上我用它来加载socket.io
<script src="/socket.io/socket.io.js"></script>
更新01: 我注意到当我在浏览器中找到不同的URL,然后回来 - 套接字正确加载... 这是否表示需要延迟......?