Google云端平台VM:https

Docker在GCP虚拟机中编写运行2个容器:

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

端口redirect设置为443,configuration了防火墙(我猜),但仍然无法通过https连接到服务器。 它仅在http:// ip_address:443上可用

我究竟做错了什么?

你做错了什么是你假设只是因为你使用的端口443stream量变成SSL。

如果端口443上的东西可以通过http://<IP>:443/ ,这意味着您正在http://<IP>:443/上运行普通的HTTP应用程序。

所以你在你的NodeJS服务器上将创build一个没有证书和私钥的简单服务器。

有两个选项,你有

在代码中使用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放在前面服务

您可以使用SSLconfiguration添加nginx,然后代理将stream量传递给您的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 

您将需要创build一个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/