Nginx 支持单域名多 Vue 服务配置备忘
最近开发时,遇到需要使用同一域名承载多个前端项目的场景,具体需求如下:
/v2
访问新版本前端项目/api
访问后端 Spring Boot 接口服务/
访问默认前端项目
1. Nginx 配置内容
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; alias /path/to/v2/web/app; } }
注意 Nginx 的 alias
配置。此时,新前端项目需要被放在 /path/to/v2/web/app
路径下。
2. 修改 publicPath 配置
仅仅通过上述配置,在访问新版前端时,会遇到资源文件无法找到的问题。
此时,可以通过对新版前端 vue.config.js
文件中的 publicPath
进行配置,以规避这一问题( 注:该方法仅适用于 Vue-Cli 3.x 构建的项目 ):
module.exports = { ... publicPath: '/v2/', ... };
参考链接
- Understanding the difference between the root and alias directives in Nginx;
- Can’t get two single page applications to run together on one server using nginx;
原文地址:https://segmentfault.com/a/1190000019571125
相关推荐
-
Node.js官方文档:到底什么是阻塞(Blocking)与非阻塞(Non-Blocking)? 框架
2019-6-15
-
(译)React hooks:它不是一种魔法,只是一个数组——使用图表揭秘提案规则 框架
2019-3-7
-
(分享)京东618:RN框架在京东无线端的实践 框架
2019-8-20
-
手撸一个自己的前端脚手架 框架
2019-9-7
-
Vuex、Flux、Redux、Redux-saga、Dva、MobX 框架
2018-12-19
-
React项目详解 框架
2019-4-15
-
小程序开发:用生还是选框架(wepy/mpvue/uni-app/taro)? 框架
2019-7-4
-
Vue.js 十五分钟入门 框架
2018-12-17
-
Angular表单验证器 框架
2018-12-14
-
15 分钟掌握 vue-next 响应式原理 框架
2019-10-14