gpt4 book ai didi

node.js - nginx、node.js + 带 SSL 的 socket.io

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:08 26 4
gpt4 key购买 nike

我在配置 nginx 和 Node 以通过 SSL 支持 socket.io 时遇到问题。

我的 nginx 配置:

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
listen 80;
listen 443 ssl;
listen [::]:80;
listen [::]:443 ssl;

access_log /var/log/nginx/livetest.log;
server_name live-test.dev www.live-test.dev;

ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 5m;

if ($ssl_protocol = "") {
rewrite ^ https://$host$request_uri? permanent;
}

location / {
proxy_pass https://live_test;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}

live_test 是在端口 6020 上运行的 node.js 的上游。在 chrome 中测试时,它确实会在轮询(失败)状态时停止。使用 wscat 时:

wscat --connect wss://live-test.dev

我收到:错误:错误:自签名证书

我想知道这里可能出了什么问题?这是我的 node.js 应用程序:

var express = require('express');
var cookie = require('cookie');
var app = express();
var http = require('http').Server(app);
var socketIo = require('socket.io');
var redis = require('redis');
var redisClient = client = redis.createClient();

io.on('connection', function(socket){
var cookies = cookie.parse(socket.handshake.headers.cookie);

console.log(cookies);
});

http.listen(6020, function(){
console.log('listening on 6020');
});

我感觉我的 node.js 应用程序中缺少某些东西。我认为自从 nginx 处理 SSL node.js 就不再需要了,但也许我错了。

是的,我正在为 SSL 使用自签名证书。 node.js/socket.io 可以使用自签名证书吗?

@更新

阅读一些内容后,我更改了我的 node.js 应用程序:

var express = require('express');
var cookie = require('cookie');
var fs = require('fs');
var app = express();
var https = require('https').Server(app, {
key: fs.readFileSync('/etc/nginx/ssl/nginx.key'),
cert: fs.readFileSync('/etc/nginx/ssl/nginx.crt'),
});
var socketIo = require('socket.io');
var redis = require('redis');
var redisClient = client = redis.createClient();

var io = new socketIo(https);

io.on('connection', function(socket){
var cookies = cookie.parse(socket.handshake.headers.cookie);

console.log(cookies);
});

https.listen(6020, function(){
console.log('listening on 6020');
});

@更新2

abcdn 的评论之后我确实尝试过使用 -n 标志的 wscat,现在出现错误:错误:错误:意外的服务器响应 (502)

而 nginx error.log 包含:2017/03/07 13:44:10 [error] 10556#10556: *140 upstream 在从上游读取响应头时过早关闭连接

@更新 3

进一步阅读后,我将我的 app.js 转回了 http。

最佳答案

var fs = require( 'fs' );
var app = require('express')();
var https = require('https');
var io = require('socket.io')(https);
var HTTPSOptions = {
cert: fs.readFileSync('path to ssl certificate file'),
key: fs.readFileSync('path to ssl key file'),
requestCert: false,
rejectUnauthorized: false,
};
HTTPSOptions.agent = new https.Agent(HTTPSOptions);
var httpsServer = https.createServer(HTTPSOptions, app);
io = io.listen(httpsServer, {
log: false
});

//io.sockets.on('connection', function (sock) {
// console.log("CONNECTED");
//});

var Redis = require('ioredis');
var redis = new Redis();

redis.psubscribe('*', function () {});

redis.on('pmessage', function (subscribed, channel, message) {
console.log("channel: " + channel);
console.log("message: " + message);
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});

httpsServer.listen(6001, function(){
console.log('Listening on Port 6001');
});

我花了 3 天的时间来找到解决方案,这是我的最终版本,效果很好。我希望它能帮助像我一样陷入困境的任何人:)

关于node.js - nginx、node.js + 带 SSL 的 socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42648879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com