簡體   English   中英

Google Cloud Platform VM:https

[英]Google Cloud Platform VM: https

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上可用

我究竟做錯了什么?

您做錯了是假設您僅由於使​​用端口443而使流量變為SSL。

如果可以通過http://<IP>:443/訪問端口443內容,則意味着您正在http://<IP>: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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM