2016-10-02 85 views
0

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

我试图将另一个节点快速应用程序连接到另一个子目录。下面是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; 

回答

0

第一如果你不需要它,不要使用regexp位置。使用简单的位置。关于你的问题 - 把/放在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; 
} 
相关问题