我有一个 VPS,在 Apache 配置 (/etc/apache2/sites-available/000-default.conf) 中指定了多个虚拟主机。
#Example config
<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
DocumentRoot /var/www/example
ServerAdmin [email protected] /cdn-cgi/l/email-protection
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
DocumentRoot /var/www/sub.example
ServerAdmin [email protected] /cdn-cgi/l/email-protection
</VirtualHost>
另外,我在同一台服务器上有 Nodejs 应用程序,运行端口 *:3000。问题是,某些 ISP 阻止该端口,用户无法使用该应用程序(例如,在我的学校,我无法访问该端口)。我希望这个应用程序将在端口 *:443 上运行,但我仍然想在其他主机上使用 Apache。有没有办法让应用程序与 Apache 位于同一端口上,并仅在用户访问域时才使用该应用程序https://socket.example.com https://socket.example.com,否则将使用 Apache。我正在添加 Apache 服务器的示例配置和 Nodejs 应用程序的一部分。
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('/etc/letsencrypt/live/socket.example.com/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/socket.example.com/cert.pem', 'utf8');
var bodyParser = require("body-parser");
var credentials = { key: privateKey, cert: certificate };
var express = require('express');
var app = express();
var port = 3000;
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(port, function () {
console.log('Server running on port: ' + port);
});
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
if ('OPTIONS' == req.method) {
res.writeHead(200);
res.end();
} else {
next();
}
});
app.get('/', function (req, res) {
res.send(400);
console.log("visit");
});
如果我忘记了什么,请告诉我,我会添加它。