2017-01-09 95 views
2

我已经配置nginx的所有请求传递到节点:与Express和nginx的Vue公司路由器的历史模式,反向代理

server { 
    listen 80; 
    server_name domain.tld; 

    location/{ 
     proxy_pass http://localhost:3000; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection 'upgrade'; 
     proxy_set_header Host $host; 
     proxy_cache_bypass $http_upgrade; 
    } 
} 

在服务器上我有运行快速哪些服务器我Vue的索引文件节点的应用程序。

app.get('/', (req, res) => { 
    res.sendFile(`${__dirname}/app/index.html`); 
}); 

我想使用HTML5历史模式Vue的路由器,所以我设置的路由器设置mode: 'history'。我安装connect-history-api-fallback,并将其设置:

const history = require('connect-history-api-fallback'); 
app.use(history()); 

的路由工作正常,如果用户首先点击http://domain.tld。但是,如果直接访问子路由或刷新页面,则会出现未找到的错误。

如何更改我的配置?

+0

使用节点应用程序来提供文件是明智吗?用它是不是更好? nginx在资源方面? – jntme

回答

2

,因为我无法获得连接历史-API的后备库的工作,我创建了一个自己:

app.use((req, res, next) => { 
    if (!req.originalUrl.includes('/dist/', 0)) { 
    res.sendFile(`${__dirname}/app/index.html`); 
    } else { 
    next(); 
    } 
}); 

时要求什么,但/dist其中脚本和图像都位于这将发送/app/index.html

0

我有同样的问题。

时的顺序是它不会工作:

app.use(express.static('web')); 
app.use(history()); 

,而是将工作时:

app.use(history()); 
app.use(express.static('web')); 

所有示例应用程序:

var express = require('express'); 
var history = require('connect-history-api-fallback'); 
var app = express(); 

app.use(history()); 
app.use(express.static('web')); 

app.listen(3002, function() { 
    console.log('Example app listening on port 3002!') 
}); 

Web文件夹我有:
index.html
和其他js,css文件。