前端部署path问题

Nginx部署特殊Path

设置Vite

主要设置内容base:"/xxx/"

以及

build: {

outDir: path.resolve(__dirname, "dist/xxx"),

emptyOutDir: true,

},

复制代码
const { defineConfig, loadEnv } = require("vite");
const path = require("path");
const fs = require("fs");
const { createVitePlugins } = require("./vite/plugins");

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_ROUTE_BASE } = env;
  if (command == "build") {
    //打包时生成版本号
    let version = {
      version: JSON.stringify(new Date().getTime()),
    };
    fs.writeFile("./public/version.json", JSON.stringify(version), () => {
      console.log("新版本号生成成功", version);
    });
  }
  return {
    base: "/xxx/",
    plugins: createVitePlugins(env, command === "build"),
    resolve: {
      alias: {
        // 设置路径
        "~": path.resolve(__dirname, "./"),
        // 设置别名
        "@": path.resolve(__dirname, "./src"),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    build: {
      outDir: path.resolve(__dirname, "dist/xxx"),
      emptyOutDir: true,
    },

    // vite 相关配置
    server: {
      port: 3251,
      host: true,
      open: true,
      proxy: {
        "/dev-api": {
          target: "http://baidu",
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, ""),
        },
      },
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === "charset") {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
    },
  };
});

设置router

主要设置 history: createWebHistory("/xxx/")

复制代码
export const createRouter = () => {
  return createRouterOrigin({
    history: createWebHistory("/xxx/"),
    routes: reRoutes,
  });
};

设置site nginx

主要设置try_files $uri /xxx/index.html;

复制代码
gzip on;
gzip_comp_level 6;
gzip_types text/css application/javascript;

server {
  listen 80;
  root /usr/share/nginx/html;
  server_tokens off;

  location /favicon.ico {
      expires 1d;
      root /usr/share/nginx/html;
  }

  # html5 mode
  location / {
    expires -1;
    try_files $uri /xxx/index.html;

    # add_header Cache-Control;
    # add_header Access-Control-Allow-Origin *;

    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    sub_filter '__PROJECT_VERSION__' '$PROJECT_VERSION';
    sub_filter '__ENV__' '$ENV';
    sub_filter '__APP_CONFIG__' '$APP_CONFIG_BASE64';
    sub_filter_once on;
  }
}
相关推荐
optimistic_chen3 小时前
【Vue3入门】vue-router 路由管理
前端·javascript·vue.js·路由·router
柯儿的天空3 小时前
WebGPU全面解析:新一代Web图形与计算API
前端·chrome·microsoft·前端框架·chrome devtools·view design
捕捉一只前端小白3 小时前
cpolar内网穿透以及微信小程序域名设置
前端·vue.js·微信小程序·小程序
wuhen_n3 小时前
ESLint + Prettier + Husky + lint-staged:建立自动化的高效前端工作流
前端·javascript·vue.js
小同志003 小时前
HTML 基础
前端·javascript·html
wuhen_n5 小时前
网络请求在Vite层的代理与Mock:告别跨域和后端依赖
前端·javascript·vue.js
用户693717500138410 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦10 小时前
Web 前端搜索文字高亮实现方法汇总
前端