RicoLiu/Blog

vue 打包部署在服务器非根目录,访问404

Opened this issue · 0 comments

vue的项目打包部署在服务器后,访问 http://xxxx.com/xxx 时,提示各种静态资源找不到。

查阅了很多资料,对 webpack 打包做修改:

config/index.js:

build: {
  assetsPublicPath: '/xxx/',
}

build/webpack.prod.js:

output: {
  publicPath: '/xxx/',
}

router/index.js:

const router = new Router({
  base: '/xxx/'
})

最后,nginx 也需要修改对应的配置

location  ^~ /xxx/{
                alias /H5/xxx/;
                try_files $uri $uri/ /xxx/index.html;
                gzip on;
                gzip_types application/javascript text/css image/jpeg image/png;
        }

这样,访问静态资源抱404的问题就解决了。