你做错了什么是你假设仅仅因为你使用端口 443 流量就变成了 SSL。
如果端口443 上的某些内容可以作为http://<IP>:443/ 访问,这意味着您正在443 上运行一个普通的HTTP 应用程序。
因此,您在 NodeJS 服务器中将创建一个没有证书和私钥的简单服务器。
你有两个选择
在代码中使用 SSL 服务器
您可以更新您的 NodeJS 代码以作为 https 服务器进行侦听。类似下面的东西
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
把nginx放在前面供服务
您可以添加一个带有 SSL 配置的 nginx,然后代理将流量传递给您的 NodeJS 应用
version: '2'
services:
db:
image: mongo:3
ports:
- "27017:27017"
api-server:
build: .
volumes:
- .:/www
- /www/node_modules
nginx:
image: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./www:/usr/local/var/www
你需要创建一个 nginx 配置文件
server {
listen 80;
listen 443 ssl;
server_name _;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
location / {
proxy_pass http://api-server:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /public {
root /usr/local/var/www;
}
}
PS:更多详情请参考https://www.sitepoint.com/configuring-nginx-ssl-node-js/