一.修改配置文件 nginx.conf ,并重新加载或重启
我的nginx版本是1.9.9
location / {
try_files $uri $uri/ /index.html;
}
原因:
打包后的dist下只有一个 index.html 文件及一些静态资源,这个是因为Vue是单页应用(SPA),只有一个index.html作为入口文件,其它的路由都是通过JS来进行跳转
接下来看一下服务器的配置,以 nginx 为例
server {
// 监听80端口
listen 80;
// 定义你的站点名称
server_name www.mycomm.com;
// 根据请求 URI 设置配置
location / {
// 站点根目录,这里为 vue 构建出来的 dist 目录
root /www/dist;
// 站点初始页为index.html 或 index.htm
index index.html index.htm;
}
}
我们现在可以根据 nginx 配置得出,当我们在地址栏输入 website.com 时,这时会打开我们 dist 目录下的 index.html 文件,然后我们在跳转路由进入到 website.com/login
关键在这里,当我们在 website.com/login 页执行刷新操作,nginx location 是没有相关配置