【发布时间】: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;
【问题讨论】: