我一直在这里挖掘有关如何在同一服务器上使用Express vhost托管多个https域的几篇文章,但是似乎我还没有找到适合我的设置的答案,所以指出我应该做什么。
这是我的设置:
我有两个域与该子域,实际上,我可以在实验室测试中拥有两个以上的域,在每个域中,我创建了运行https的自证书。
我创建了主虚拟主机脚本,以启动通过app.js在节点上运行的所有域网站。这是代码:
// app.js for web proxy vhost .
var express = require('express');
var vhost = require('vhost');
var app = express();
var compression = require('compression');
var spdy = require('spdy');
app.use(compression());
app.use(vhost('example1.com', require('/websites/example1/app.js')))
.use(vhost('www.example1.com', require('/websites/example1/app.js')))
.use(vhost('sub1.example1.com',require('/websites/example1/sub1/app.js')))
.use(vhost('sub2.example1.com',require('/websites/example1/sub2/app.js')))
.use(vhost('example2.com', require('/websites/example2/app.js')))
.use(vhost('www.example2.com', require('/websites/example2/app.js')))
.listen(80);
});
app.get('/', function(req, res){
res.send('Server running');
});
在网站的每个域文件夹中,我都有certs文件夹,dist用于静态内容和默认路由。这是网站启动脚本的代码
//app.js for example1.com
let logger = require('morgan');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let proxy = require('http-proxy-middleware');
let appRoutes = require('./routes/app');
let app = express();
let https = require('https');
let compression = require('compression');
let fs = require('fs');
let forcessl = require('express-force-ssl');
let spdy = require('spdy');
// setup ssl
var key = fs.readFileSync('./example1/certificates/private.key');
var cert = fs.readFileSync('./example1/certificates/private.crt');
var ca = fs.readFileSync('./example1/certificates/root.ca');
var options = {
key: key,
cert: cert,
ca: ca
};
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(compression({filter: compressContent}));
function compressContent(req, res){
if(req.header['x-no-compression']){
return false
}
return compression.filter(req,res)
}
// uncomment after placing your favicon in /public
app.use(forcessl);
app.use(favicon(path.join(__dirname, 'dist', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'dist')));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'https://example1.com');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
next();
});
app.use('/api/**', proxy({target: 'http://localhost:8080', changeOrigin: false}));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
return res.render('index');
});
spdy.createServer(options, app)
.listen(443, function(){
console.log('listening on port 443! https://example1.com');
})
console.log('Listening on port 80 http://example1.com');
module.exports = app;
在域example2.com上,我还创建了类似于example1.com的启动脚本。这是代码:
//app.js for example2.com
let logger = require('morgan');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let proxy = require('http-proxy-middleware');
let appRoutes = require('./routes/app');
let app = express();
let https = require('https');
let compression = require('compression');
let fs = require('fs');
let forcessl = require('express-force-ssl');
let spdy = require('spdy');
// setup ssl
var key = fs.readFileSync('./example2/certificates/private.key');
var cert = fs.readFileSync('./example2/certificates/private.crt');
var ca = fs.readFileSync('./example2/certificates/root.ca');
var options = {
key: key,
cert: cert,
ca: ca
};
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(compression({filter: compressContent}));
function compressContent(req, res){
if(req.header['x-no-compression']){
return false
}
return compression.filter(req,res)
}
// uncomment after placing your favicon in /public
app.use(forcessl);
app.use(favicon(path.join(__dirname, 'dist', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'dist')));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'https://example2.com');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
next();
});
app.use('/api/**', proxy({target: 'http://localhost:8081', changeOrigin: false}));
// catch 404 and forward to error handler
app.use(function (req, res, next) {
return res.render('index');
});
spdy.createServer(options, app)
.listen(443, function(){
console.log('listening on port 443! https://example2.com');
})
console.log('Listening on port 80 http://example2.com');
module.exports = app;
我现在遇到的问题是,我无法创建第二个https服务器,无法在example2.com域的端口443上进行侦听,但第一个服务器由于端口冲突而在exmaple1.com上侦听了443,因为端口冲突。
如何在example1.com和example2.com上同时运行https?注意:我将spdy用于https和http2协议,而不是普通的https。