-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
前端build之后如何部署到nginx二级目录中,API 路径怎么修改? #725
Comments
以上两个参数是关于, 后端请求的路径, 跟前端无关. https://vitejs.dev/guide/build.html#public-base-path chatgpt 答案 可以在 假设要将构建后的应用部署在 export default {
// ...
base: '/my-app/'
} 这样,在 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vite App</title>
<link rel="icon" type="image/svg+xml" href="/my-app/favicon.svg" />
<link rel="stylesheet" href="/my-app/assets/css/app.abc123.css" />
</head>
<body>
<div id="app"></div>
<script type="module" src="/my-app/assets/js/app.abc123.js"></script>
</body>
</html> 注意, export default {
// ...
base: '/my-app/',
server: {
// dev server 配置中要相同
port: 8080,
base: '/my-app/'
}
} 这样,在启动开发服务器时,应用即会在 import { createRouter, createWebHistory } from 'vue-router';
const router = createRouter({
history: createWebHistory('/my-app/'), // 添加 base 配置
routes: [
{
path: '/',
component: () => import('./views/Home.vue')
},
{
path: '/about',
component: () => import('./views/About.vue')
}
]
}); 这样,在使用路由导航时,即可正确地跳转到对应路径。 |
按照你的提示,已经可以通过localhost:1002/my-app/ 的方式访问了,但是通过nginx反代之后就404了,nginx这么配置有问题吗(本人小白)
|
前端代理成功了,浏览器请求的时候,路径末尾少了/ 导致请求失败。也可以把vite.config.ts中base路径末尾的/去掉,即
修改前端环境变量 .env
|
修改成功了,写一下我的配置:
然后,需要前端打包修改一个文件
这样可以通过 |
VITE_GLOB_API_URL=/api #这个修改 好像并不起作用
VITE_APP_API_BASE_URL=http://localhost:3002/
The text was updated successfully, but these errors were encountered: