nginx部署html5项目,vue多项目nginx部署

vue多项目nginx部署

原创

ljpwinxp2018-04-13 09:21:31©著作权

©著作权归作者所有:来自51CTO博客作者ljpwinxp的原创作品,如需转载,请注明出处,否则将追究法律责任

https://blog.51cto.com/191226139/2102483

# 项目目的

实现服务端同一域名下部署多个vue项目。

## 网站目录结构如:

```

根/

├── index.html

├── mms

└── wap

```

## 项目环境

```

系统平台:

CentOS Linux release 7.4.1708 (Core) 内核 3.10.0-693.el7.x86_64

nginx version: nginx/1.12.2

```

### 排查过程

前端在打包静态文件的时候,只把assetsPublicPath: '/wap' 修改为对应的子目录

那么尝试nginx各种写法,均未成功。

只写这个,全部指向/

```

location /wap {

try_files $uri $uri/ /wap/index.html;

```

```

错误日志

2018/04/13 08:10:16 [error] 74906#0: *8 open() "/usr/share/nginx/html/static/js/index.b5c514831ef6db6a3e00.js" failed (2: No such file or directory), client: 192.168.10.136, server: _, request: "GET /static/js/index.b5c514831ef6db6a3e00.js HTTP/1.1", host: "192.168.10.247", referrer: "http://192.168.10.247/wap/"

```

这种写法,内部500 Internal Server Error

```

location /wap {

root /usr/share/nginx/html/wap;

index index.html index.htm;

try_files $uri $uri/ @router;

}

location @router {

rewrite ^.*$ /wap/index.html last;

}

```

同样错误,全部指向/

```

location /wap {

root /usr/share/nginx/html/wap;

index index.html index.htm;

try_files $uri $uri/ @router;

}

location @router {

rewrite ^.*$ /index.html last;

}

```

```

错误日志

2018/04/13 08:27:54 [error] 76039#0: *42 open() "/usr/share/nginx/html/static/js/index.e63d3efadf103006619e.js" failed (2: No such file or directory), client: 192.168.10.136, server: _, request: "GET /static/js/index.e63d3efadf103006619e.js HTTP/1.1", host: "192.168.10.247", referrer: "http://192.168.10.247/wap/"

```

这种写法,也是内部500 Internal Server Error

```

location /wap {

root /usr/share/nginx/html/wap;

index index.html index.htm;

try_files $uri $uri/ /wap/index.html;

}

location @router {

rewrite ^.*$ /index.html last;

}

```

内部500 Internal Server Error

```

location /wap {

root /usr/share/nginx/html/wap;

index index.html index.htm;

try_files $uri $uri/ /wap/index.html;

}

location @router {

rewrite ^.*$ /wap/index.html last;

}

```

![image](https://s4.51cto.com/images/blog/201804/13/4a3acfeb0c5b13d7c79ef2c02bbb23b0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

因为我也不懂前端的事情,查了下资料,应该是开发那边的环境生成的路由有误。

## 修改vue生成参数

```

1. index.html文件修改

添加

2. config/index.js文件修改

修改 assetsPublicPath: '/子目录名/'

3.src/router/index.js文件修改

添加 base: '/子目录名/'

```

## Nginx配置

```

location /子目录名 {

try_files $uri $uri/ @router;

}

location @router {

rewrite ^.*$ /子目录名/index.html last;

}

```

![image](https://s4.51cto.com/images/blog/201804/13/1e13592d58d9bbbc35af13f0d12aa565.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

成功。

小坑坑,大家注意即可,并不是什么都是运维的问题,更加需要大家一起合作解决问题。这才是团队。

1赞

收藏

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值