【问题标题】:NodeJS Express and Apache on same serverNodeJS Express 和 Apache 在同一台服务器上
【发布时间】: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 应用程序。

标签: node.js apache express


【解决方案1】:

创建一个 Apache 虚拟主机,它将代理 node.js 应用程序的请求。您可以将 node.js 应用程序运行到任何可用端口,并通过 apache 代理它。

<VirtualHost *:443>
   ServerName yourdomain.com
   ServerAlias www.yourdomain.com
   DocumentRoot /var/www/defaultapp/
   Options -Indexes
   ErrorDocument 503 /check.html

   SSLProxyEngine On
   ProxyPass /check.html !
   ProxyPass / https://localhost:3000
   ProxyPassReverse / https://localhost:3000
   ProxyPreserveHost On

   SSLEngine on
   SSLCertificateFile /etc/apache2/ssl/domain.com.crt
   SSLCertificateKeyFile /etc/apache2/ssl/domain.com.key
   SSLCertificateChainFile /etc/apache2/ssl/domain.com:3000.ca-bundle
</VirtualHost>

详情请查看https://linuxtogether.org/configuring-reverse-proxy-for-node-using-apache-mod-proxy/

【讨论】:

  • 感谢您的建议,也许这可能是我一直在寻找的解决方案,但是使用该配置,页面显示“禁止您无权访问 / 在此服务器上。” .我尝试启用/禁用某些模块,将“要求所有已授予”添加到配置机器人对我没有帮助。 Apache2 日志:[Wed Jul 11​​ 20:04:39.088714 2018] [authz_core:error] [pid 3109] [client x] AH01630:客户端被服务器配置拒绝:代理:socket.example.com:3000
  • 尝试在 http 上运行 node.js 应用程序并从 apache 提供 https。另外,确保启用了 proxy 和 proxy_http 模块(启用模块 a2enmod proxy a2enmod proxy_http)
  • 两个模块都已启用,尝试了我在 stackoverflow 上找到的所有解决方案,但其中大部分是关于我之前尝试过的“要求所有已授予”
猜你喜欢
  • 2016-09-06
  • 2012-04-07
  • 1970-01-01
  • 2018-12-04
  • 2016-07-23
  • 2019-08-11
  • 2014-03-27
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多