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

标签 ssl docker https google-cloud-platform docker-compose

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/

相关文章:

networking - 调用 tcp : lookup xxx. xxx.xxx.xxx: 没有这样的主机

c# - Mono https webrequest 失败,返回 "The authentication or decryption has failed"

amazon-web-services - AWS Elastic Beanstalk 多容器 Docker 配置上的 HTTPS/SSL 问题

java - Volley SSL - 主机名未验证

docker - 主机是否可以通过Docker容器上运行的KDC进行身份验证?

java - 将技能部署为 Web 服务(不使用 ngrok 来隧道化本地主机)

nginx - 使用 nginx 保护 https 以获取私有(private) IP 地址

ruby - SSL_connect returned=1 errno=0 state=未知状态 : unknown protocol

ruby-on-rails - Heroku 控制台中的预期 (200) <=> 实际 (401 未经授权)

php - 如何在AWS Elastic Beanstalk Docker容器中记录PHP错误