首页 > 解决方案 > 如何从 ssl 网站通过 https 加载 socketio?

问题描述

我有一个在端口 3001 上成功运行的 HTTP nodejs/socketio 服务器。我正在尝试从使用 certbot 加密的 SSL 网站访问服务器(让我们加密)。

当我访问时https//:example.com/index.html,我看到 404 错误:

获取https://www.example.com:8000/socket.io/socket.io.js 网络::ERR_ABORTED 404(未找到)

我一直在这里这里阅读有关使用 mod_proxy 配置 apache 的信息。但是,根据nodejs 文档,所需要的只是我拥有的 pem 文件。

我觉得我在追逐自己的尾巴,我需要帮助。我花了将近一周的时间进行研究并尝试不同的方法。我回到了第一广场。有人运行 https 服务器并通过 SSL 网站成功访问它吗?以下是所有相关代码。

服务器.js

const express = require('express');
const app = express();
const https = require('https');
const fs = require('fs');

// This line is from the Node.js HTTPS documentation.
var options = {
    key: fs.readFileSync('../ssl/privkey.pem'),//etc/letsencrypt/
    cert: fs.readFileSync('../ssl/cert.pem')
  };

const sslserver = https.createServer(options,app);

//establish connection
io.on('connection', (socket) => {

//requests

});

//listening for https
sslserver.listen(8000, () => {
console.log('listening on *: 8000');
});

索引.html

<!DOCTYPE html>
<html>

<head>

<title>My Web App </title>

<meta charset="utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="initial-scale=1, width=device-width, viewport-fit=cover">
<meta name="color-scheme" content="light dark">
</head>

<body >


<p>Welcome to web app</p>
<p>login now</p>

</body>

</html>
<!--SCRIPTS-->
<script src="cordova.js"></script>
<script src="https://www.example.com:8000/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect('https://www.example.com:8000', {'multiplex': false});
</script>

标签: node.jshttpssocket.iohttp-status-code-403

解决方案


如果你在 server.js 中使用凭据,你应该监听端口 443。否则,你只需要监听你的自定义端口,然后为它创建一个反向代理。

例如:

server.js(直接听443/80)

const express = require('express');
const app = express();
var http = require('http');
const https = require('https');
const fs = require('fs');

// This line is from the Node.js HTTPS documentation.
var options = {
    key: fs.readFileSync('../ssl/privkey.pem'), //etc/letsencrypt/
    cert: fs.readFileSync('../ssl/cert.pem')
};

const port = process.env.PORT || 80;
const sercure_port = 443;

// use on server with https

var httpServer = http.createServer((req, res) => {
    res.writeHead(301, { Location: `https://${req.headers.host}${req.url}` });
    res.end();
});
http.createServer(function (req, res) {
    res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url});
res.end();
})
var httpsServer = https.createServer(options, app);

const io_server = require('socket.io')(httpsServer);

httpServer.listen(port, () => {
    console.log('http server listening on port ' + port);
});

httpsServer.listen(sercure_port, () => {
    console.log('https server listening on port ' + sercure_port);
});

server.js(在自定义端口监听)

const express = require('express');
const app = express();
const http = require('http');
const fs = require('fs');
var options = {
    key: fs.readFileSync('../ssl/privkey.pem'), //etc/letsencrypt/
    cert: fs.readFileSync('../ssl/cert.pem')
};
var port = process.env.PORT || 8080;

http.createServer(function (req, res) {
    res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url});
    res.end();
})
var httpsServer = http.createServer(options, app);

const io_server = require('socket.io')(httpsServer);

httpsServer.listen(port, () => {
    console.log('https server listening on port ' + port);
});

NGINX 保留代理

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    

    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }

    ssl_certificate path-to-crt.crt;
    ssl_certificate_key path-to-key.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

   #Load configuration files for the default server block.
   include /etc/nginx/default.d/*.conf;

   error_page 404 /404.html;
       location = /40x.html {
   }

   error_page 500 502 503 504 /50x.html;
       location = /50x.html {
   }
}

推荐阅读