前提:只听过没用过,对 nginx 有基础认识
现在想做基于代码分支的测试环境自动化管理,具体而言:
- 存在一个前端仓库,有 feat1 、feat2 ;存在一个后端仓库,有 feata 、featb
- 将前端构建产物放在 front/feat1 、front/feat2 中;将后端产物放在 backend/feata(端口 8001)、backend/featb(端口 8002)中并启动
- 页面上通过 cookie 指定前后端分支,比如需求 1 对应 front/feat1 & backend/feata
- 在 nginx 中通过 cookie 反向代理到对应的服务,需要写类似配置:
upstream hilinux_01 {
server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
upstream hilinux_02 {
server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
}
upstream default {
server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
server {
listen 80;
server_name www.hi-linux.com;
access_log logs/www.hi-linux.com.log main;
#match cookie
set $group "default";
if ($http_cookie ~* "version=V1"){
set $group hilinux_01;
}
if ($http_cookie ~* "version=V2"){
set $group hilinux_02;
}
location / {
proxy_pass http://$group;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
}
有新的分支部署时需要修改 nginx 配置,openresty 可以根据 cookie 的 value 去 redis 中取出匹配的 ip:port 作为反向代理的目标机器吗?有没有相关资料或者案例可以模仿呢?