【问题标题】:Multiple node apps with NGINX in sub-directories子目录中带有 NGINX 的多节点应用程序
【发布时间】:2016-10-02 22:46:56
【问题描述】:

我目前有一个连接到域的数字海洋水滴。在服务器上,我正在运行 NGINX 并尝试将多个节点应用程序反向代理到它。目前,我的根目录有一个 node express 应用程序,位于 / 。

我正在尝试将另一个 node express 应用程序连接到另一个子目录。这是 nginx 配置文件:

server {
    listen 80;

    server_name servername.com;

    # my root app
    location / {
        proxy_pass http://127.0.0.1:6001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    # new app
    location ~^ /newapp {
        proxy_pass http://127.0.0.1:6002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

问题是新应用程序试图从 /newapp 中提供文件,这会中断。我认为这可能是我的 app.js 文件中的一些内容,可以在新应用程序中使用 Express,将基本目录设置为 /newapp/ - 以从那里提供静态文件和路由。关于如何做到这一点的任何想法?

在 newapp 中,我正在提供静态文件:

// Serve files out of ./public
app.use(express.static(__dirname + '/public'));

并将路由文件设置为:

var index = require('./routes/index');
app.use('/', index);

带索引路由文件:

var express = require('express');
var router = express.Router();

// Get index page
router.get('/', function(req, res, next) {
    res.render('index', {
        index : 'active'
    });
});

module.exports = router;

【问题讨论】:

    标签: node.js express nginx


    【解决方案1】:

    首先,如果不需要,不要使用正则表达式位置。使用简单的位置。关于您的问题 - 将 / 放在 proxy_pass URI 的末尾。 Nginx 会将 /newapp/xxx 重写为 /xxx ,反之亦然(例如,对于 http 重定向)。但是 (!) 不会重写 HTML 正文中的链接。

    location /newapp/ {
        proxy_pass http://127.0.0.1:6002/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多