【发布时间】:2018-07-11 17:07:24
【问题描述】:
我有一个在 Apache 配置 (/etc/apache2/sites-available/000-default.conf) 中指定了多个虚拟主机的 VPS。
#Example config
<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
DocumentRoot /var/www/example
ServerAdmin admin@example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
DocumentRoot /var/www/sub.example
ServerAdmin admin@example.com
</VirtualHost>
另外,我在使用端口 *:3000 运行的同一台服务器上安装了 Nodejs 应用程序。问题是,某些 ISP 阻止了该端口并且用户无法使用该应用程序(例如,在我的学校中,我无法访问该端口)。我希望这个应用程序将在端口 *:443 上运行,但我仍然想将 Apache 用于其他主机。有什么方法可以让应用程序与 Apache 位于同一端口上,并且只有在用户访问域 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");
});
如果我忘记了什么,请告诉我,我会补充的。
【问题讨论】:
-
在不同的端口上运行 node.js,并从 apache 代理对 node.js 的请求。检查digitalocean.com/community/tutorials/…
-
我会看看那个,但我之前用谷歌搜索过并找到了这个解决方案,但是当我输入时它可以工作,例如“/node”在任何域之后,我只想在特定域时连接应用程序。什么是不同的端口?保持:3000 还是?
-
在 Apache 中创建一个虚拟主机,它将侦听端口 443,然后 proxy_pass 请求可以在端口
3000上运行的 node.js 应用程序。