1、如果想要nginx的配置location / 是 location /myapp 这样的,那么必须在vite.config.ts配置中添加
//添加配置:base:'/project1'
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
const env = loadEnv(mode, process.cwd());
return {
base:'/myapp/',
// 其他代码逻辑......
},
base:'/myapp' // 此处和URL地址是匹配的
// 注意,base 路径必须以斜杠 / 开始和结束,以确保路径正确地应用于所有的资源 URL
这样访问地址就是http://ip地址/myapp/的访问地址
开发环境的相关配置:
## 开发环境
# 变量必须以 VITE_ 为前缀才能暴露给外部读取
NODE_ENV='development'
VITE_APP_TITLE = 'vue3-element-admin'
VITE_APP_PORT = 9000
# API请求前缀
VITE_APP_BASE_API = '/dev-api'
# proxy代理配置
# VITE_APP_TARGET_URL = 'http://vapi.youlai.tech' # 线上接口
VITE_APP_TARGET_URL = 'http://localhost:8080' # 本地接口,本地启动后端:https://gitee.com/youlaiorg/youlai-boot
# VITE_APP_TARGET_URL = 'http://localhost:9000' # 本地Mock
VITE_APP_TARGET_BASE_API = ''
nginx相关配置:
server {
listen 9000;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /safeEnv {
alias D:/soft/myapp;
index index.html index.htm;
try_files $uri $uri/ /myapp/index.html;
}
# API请求代理
location /dev-api {
proxy_pass http://localhost:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、如果需要nginx的配置后面只是location /
那么vite.config.ts配置文件中不需要添加base路径,
nginx的相关配置是
server {
listen 9000;
server_name localhost;
location / {
root D:/soft/myapp;
index index.html index.htm;
try_files $uri $uri/ /myapp/index.html;
}
# API请求代理
location /dev-api {
proxy_pass http://localhost:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
附加:windows10下cmd的nginx 相关命令
// 启动命令,会闪出一个黑窗口然后自动关闭
start nginx
// 停止nginx服务
nginx -s stop
// 运行命令停止不掉,只能用更暴力的方式了
taskkill /f /t /im nginx.exe
// 查看nginx是否启动
tasklist /fi "imagename eq nginx.exe"
// 重启nginx
nginx -s reload