用的是express模板。下載的是阿裡雲Nginx證書。配完nginx.conf,可以用 nginx -t; 檢查一下,只要提示is ok和successful就行, 然後重啟用 sudo service nginx reload; 如果提示‘Redirecting to /bin/systemct ...
用的是express模板。下載的是阿裡雲Nginx證書。配完nginx.conf,可以用 nginx -t; 檢查一下,只要提示is ok和successful就行,
然後重啟用 sudo service nginx reload; 如果提示‘Redirecting to /bin/systemctl reload nginx.service’,沒有關係。
重點證書不僅要放在Nginx里,項目也是要讀取的,沒寫項目的部分,導致一直連不上,以此為戒。
http{ #nginx.conf里的一些基本配置,此處省略,重點是以下部分 #http配置 upstream aaa.com{ ip_hash; server 127.0.0.1:4000; } server { listen 80; server_name aaa.com www.aaa.com; location /{ proxy_pass http://aaa.com; root /home/officialWebsite/public; index index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # https配置 upstream bbb.com { server 127.0.0.1:4001; } server { listen 443 ssl; # listen [::]:443 ssl; server_name bbb.com; ssl_certificate /etc/nginx/xxx.pem; # 替換為你的SSL證書路徑 ssl_certificate_key /etc/nginx/xxx.key; # 替換為你的SSL私鑰路徑 location / { proxy_pass https://bbb.com; root /home/officialWebsite/public; index index.html; #proxy_set_header Host $host; #proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header X-Forwarded-Proto $scheme; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } }
bin下的www要開2個埠,一個https,一個http指向。
#!/usr/bin/env node /** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('officialwebsite:server'); var http = require('http'); var https = require('https'); var fs=require("fs"); var path = require('path'); /** * Get port from environment and store in Express. */ var httpPort = '4000';//http的埠號 var httpsPort = '4001';//https的埠號 var port = normalizePort(process.env.PORT || httpPort); app.set('port', port); /** * Create HTTP server. */ var server = http.createServer(app); /** * Listen on provided port, on all network interfaces. */ server.listen(port); server.on('error', onError); server.on('listening', onListening); /** * Normalize a port into a number, string, or false. */ function normalizePort(val) { var port = parseInt(val, 10); if (isNaN(port)) { // named pipe return val; } if (port >= 0) { // port number return port; } return false; } /** * Event listener for HTTP server "error" event. */ function onError(error) { if (error.syscall !== 'listen') { throw error; } var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'EADDRINUSE': console.error(bind + ' is already in use'); process.exit(1); break; default: throw error; } } /** * Event listener for HTTP server "listening" event. */ function onListening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); console.log('Listening on ' + bind); } var httpsweb={ key:fs.readFileSync(path.join(__dirname, 'xxx.key')),//# 替換為你的SSL私鑰路徑 cert:fs.readFileSync(path.join(__dirname, 'xxx.pem')),//# 替換為你的SSL證書路徑 }; var sserver = https.createServer(httpsweb, app); sserver.listen(httpsPort); console.log('https',httpsPort)