前端部署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;
  }
}
相关推荐
沐言人生3 小时前
自媒体人的效率神器!监控、采集、下载、发布,这个免费开源工具全拿下!
前端·vue.js·后端
excel4 小时前
为什么 super 中能接收到 resolve 和 reject?
前端
Flynt6 小时前
AI 生成的界面,能放心点吗?我把 Google A2UI 的三层防线拆开测了一遍
前端·google·agent
前端混子7 小时前
我用 AI 做了个「禾月小历」,一套代码跑微信小程序和 H5
前端
IT_陈寒8 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
小帅不太帅8 小时前
一年一次黑五,为了买软件真省钱,我做了个开源小站
前端·人工智能·github
JavaGuide10 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang021710 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
用户693717500138410 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github
skywalk816310 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness