【问题标题】:Google Cloud Platform VM: https谷歌云平台虚拟机:https
【发布时间】:2017-08-06 20:28:10
【问题描述】:

Docker 组合在 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

我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    你做错了什么是你假设仅仅因为你使用端口 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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2016-05-08
      • 1970-01-01
      • 2018-09-23
      相关资源
      最近更新 更多