2017-08-06 169 views
0

碼頭工人組成運行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

我在做什麼錯?

回答

1

你在做什麼錯誤的是,你假設只是因爲你使用端口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/