项目场景:
这篇文章仅是记录自己成长路上的愚笨,不喜勿喷,同时也希望各路大神提出优秀的观点。谢谢!!
问题描述:
项目是使用前后端分离,前端使用的是vue技术。
想实现的功能是:在前端项目部署的过程中(直接删除项目目录的文件,然后上传新的项目文件),在这个短时间内不能影响用户的使用。所以想到的就是使用nginx搭建web集群。
nginx配置文件:
# 页面集群地址
upstream house_keeper_html {
server 127.0.0.1:9089;
server 127.0.0.1:9088;
ip_hash; # 负载均衡策略
}
# 页面集群转发
server {
listen 9090; # 向外暴露的端口(浏览器直接请求的地址端口)
location / {
proxy_pass http://house_keeper_html; #把所有请求转发
}
}
# 页面集群地址1
server {
listen 9089;
location / {
root /home/web_programs/house_keeper/property/back-html5/;
index index.html index.html;
try_files $uri $uri/ /index.html;
error_page 403 404 @notfound; # 当遇到403或404(项目部署时页面删除的时候)进行转发
}
location @notfound {
proxy_pass http://127.0.0.1:9088; # 转发的地址
}
}
# 页面集群地址2
server {
listen 9088;
location / {
root /home/web_programs/house_keeper/property/back-html5_1/;
index index.html index.html;
try_files $uri $uri/ /index.html;
error_page 403 404 @notfound; # 同理进行转发
}
location @notfound {
proxy_pass http://127.0.0.1:9089;
}
}
思路:
其实就是使用了nginx的错误页面重定向的思路,在其中一台报404的时候nginx检测到后进行转发到另一台。