【发布时间】:2017-05-23 13:57:15
【问题描述】:
我已将 nginx 配置为将所有请求传递给 Node:
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;
}
}
在服务器上,我有一个运行 Express 的 Node 应用程序,它为我的 Vue 索引文件提供服务。
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/app/index.html`);
});
我想在 Vue 路由器中使用 HTML5 历史模式,所以我在路由器设置中设置了mode: 'history'。我安装了connect-history-api-fallback 并进行了设置:
const history = require('connect-history-api-fallback');
app.use(history());
如果用户第一次点击http://domain.tld,路由就可以正常工作。但是,如果直接访问子路由,或者刷新页面,我会得到一个未找到的错误。
如何更改我的配置?
【问题讨论】:
-
使用节点应用程序提供文件是否明智?使用例如不是更好吗? nginx 在资源方面?
标签: node.js express nginx vue-router