【问题标题】:How to set up Apache ProxyPass to preserve Express routes如何设置 Apache ProxyPass 以保留 Express 路由
【发布时间】:2016-01-14 03:41:53
【问题描述】:

在我的 Apache 配置中,我将 /node 上的所有流量转发到 Express 服务器正在侦听的端口 3000

<IfModule mod_proxy.c>

  ProxyRequests Off

  ProxyPass /node http://localhost:3000/

</IfModule>

Express 应用如下所示:

var express = require('express');

var app = express();
var router = express.Router();

router.route('/route/:id').get(function (req, res) {

    res.json({ description: 'Welcome to a route with an ID' }); 
});

router.route('/route').get(function (req, res) {

        res.json({ description: 'Welcome to the normal route' }); 
});

router.route('/').get(function (req, res) {

    res.json({ data: 'Welcome to the app' }); 
});

app.use('/', router);

app.listen(3000);

当我将浏览器定向到http://myserver.com/node 时,我得到了{ data: 'Welcome to the app' } 的响应,这很好。不过,当我尝试使用 http://myserver.com/node/routehttp://myserver.com/node/1210 时,我收到错误 Cannot GET //route

有什么想法可以更新我的 Apache 配置以保留 Express 路由吗?

我在 CentOS 上运行 Apache 2.4.6。

【问题讨论】:

  • 只是提示:用的是nginx代理节点,没想过用nginx来apache inves?
  • 谢谢!但不幸的是,使用 nginx 不是一种选择。

标签: node.js apache express httpd.conf


【解决方案1】:

您的主机末尾有一个额外的/。尝试将其更改为:

ProxyPass /node http://localhost:3000

【讨论】:

  • 非常感谢。那成功了。现在我自己去打脸。
【解决方案2】:

在使用我的 nodejs 后端系统在 apache 2 服务器上设置虚拟主机后,我遇到了同样的问题,这是我修复它的方法:

    ProxyRequests On
    ProxyVia Full
    ProxyPreserveHost on
    ProxyPass /api http://jwappengine.com:7000
    ProxyPass /secure http://jwappengine.com:1442
    ProxyPass /host https://jwappengine.com:1400
    ProxyPass /connect/v1 https://jwappengine.com:1400
    ProxyPass /payment https://jwappengine.com:3000

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 2016-04-20
    • 2016-01-11
    • 2018-05-13
    • 2022-01-17
    • 2020-10-05
    • 2020-06-17
    • 2020-06-16
    相关资源
    最近更新 更多