简体   繁体   English

适用于SockJS + Express的Nodejs SSL

[英]Nodejs SSL for SockJS + Express

I've got a simple SockJS and Express server in nodejs . 我有一个简单的SockJSExpress的服务器nodejs Now id like to add SSL support for these servers. 现在,id喜欢为这些服务器添加SSL support

Here is my server code: 这是我的服务器代码:

var sockjs = require('sockjs');
var my_http = require("http");
var https = require('https');
var fs = require('fs');


var options = {
    key: fs.readFileSync('test/keys/key.pem'),
    cert: fs.readFileSync('test/keys/cert.pem')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(8008);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);


var echo = sockjs.createServer({
    log: function (severity, message) {}
});
echo.on('connection', function (conn) {

    conn.on('data', function (message) {
        conn.write(message);
    });

    conn.on('close', function () {

    });
});

var server = my_http.createServer();
echo.installHandlers(server, {
    prefix: '/echo'
});
server.listen(8081, '0.0.0.0');

var server_https = my_http.createServer(options);
echo.installHandlers(server_https, {
    prefix: '/echo'
});
server_https.listen(443, '0.0.0.0');



app.get('/type/:channel', function (req, res) {
    res.setHeader('Content-Type', 'application/json');
    res.send("Hello");

    res.end();
});

Problem is that i get the port already in use error : 问题是我得到该port already in use error

Error: listen EADDRINUSE

I've got Nginx listening on 443 otherwise my site would not work on ssl. 我让Nginx监听443,否则我的网站无法在ssl上运行。

Any ideas how to set this up? 任何想法如何设置?

Inside of your nginx config you should have your port listed in 'upstream'. 在您的nginx配置中,您应该在“上游”中列出您的端口。 In your case you probably have the same port listed under server. 在您的情况下,您可能在服务器下列出了相同的端口。 It shows that error when you do that. 当您这样做时,它将显示该错误。 See below for proper configuration (If you change "listen 80" to "listen 8000" you'll see that error): 参见下面的正确配置(如果您将“ listen 80”更改为“ listen 8000”,则会看到该错误):

    upstream app_yourAppName {
        server 127.0.0.1:8000;
    }

    # the nginx server instance
    server {
        listen 80;
        ...
        ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM