鍍金池/ 問(wèn)答/HTML/ 關(guān)于vue部署到nginx服務(wù)下,非根目錄,刷新頁(yè)面404怎么解決?

關(guān)于vue部署到nginx服務(wù)下,非根目錄,刷新頁(yè)面404怎么解決?

將vue項(xiàng)目部署到nginx遇到了刷新頁(yè)面跳轉(zhuǎn)到404錯(cuò)誤頁(yè)面的問(wèn)題,項(xiàng)目不是部署在根目錄下面。有相關(guān)同學(xué)遇到過(guò)類似情況嗎。我已經(jīng)安裝官網(wǎng)的配置 try_files $uri $uri/ /index.html;

clipboard.png

nginx配置

location / {
          proxy_pass         http://xxxx;
    }

    location /category {
            root        /home/tv;
            index       index.html;
            try_files $uri $uri/ /index.html;
    }

webpack 配置

 build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/category/',
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report

},

回答
編輯回答
悶油瓶

nginx配置如下

rewrite ^.+(?<!js|css|png|map)$ /index.html break;

把你的那個(gè)try_files替換掉

2018年5月30日 04:14
編輯回答
女流氓

正確配置

 location /category {
        root        /home/tv;
        index       index.html;
        try_files $uri $uri/ /category/index.html;
}
2017年5月10日 19:34
編輯回答
初念

看到你有修改vue-router的mode

改回來(lái)刷新再試試吧

========================

圖片描述

2018年6月16日 12:23
編輯回答
執(zhí)念

除了上面提到的,應(yīng)該在webpack的配置中要添加{publicPath: '/category/'}

2017年6月19日 22:08