gpt4 book ai didi

ssl - 谷歌云平台虚拟机 : https

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:17 24 4
gpt4 key购买 nike

Docker compose 在 GCP VM 中运行 2 个容器:

version: '2'
services:
db:
image: mongo:3
ports:
- "27017:27017"
api-server:
build: .
ports:
- "443:8080"
links:
- db
volumes:
- .:/www
- /www/node_modules

端口重定向设置为 443,配置了防火墙(我猜),但我仍然无法通过 https 连接到服务器。它仅适用于 http://ip_address:443

我做错了什么?

最佳答案

您做错的是您假设仅仅因为您使用的是端口 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 conf 文件

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/

关于ssl - 谷歌云平台虚拟机 : https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45531758/

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