最近开发时,遇到须要使用同一域名承载多个前端项目的场景,具体需求以下:html
/v2
访问新版本前端项目/api
访问后端 Spring Boot 接口服务/
访问默认前端项目
server { listen 80; listen [::]:80; server_name _; server_name_in_redirect off; proxy_set_header Host $host; location /api { proxy_pass http://0.0.0.0:0000; } location / { index index.html; root /path/to/main/web/app; } location /v2 { index index.html; root /path/to/v2/web/app; } }
仅仅经过上述配置,在访问新版前端时,会遇到资源文件没法找到的问题。前端
此时,能够经过对新版前端 vue.config.js
文件中的 publicPath
进行配置,以规避这一问题( 注:该方法仅适用于 Vue-Cli 3.x 构建的项目 ):vue
module.exports = { ... publicPath: '/v2/', ... };