谷歌云平台虚拟机:HTTPS
问题描述:
码头工人组成运行2个容器GCP VM内:谷歌云平台虚拟机:HTTPS
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将创建无证书和私钥一个简单的服务器。
有两个选项,你必须在代码
您可以更新您的代码的NodeJS听为HTTPS服务器
使用SSL服务器。类似下面
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/