请问 openresty 可以实现这个功能吗?

2021-11-30 14:44:11 +08:00
 vevlins

前提:只听过没用过,对 nginx 有基础认识

现在想做基于代码分支的测试环境自动化管理,具体而言:

  1. 存在一个前端仓库,有 feat1 、feat2 ;存在一个后端仓库,有 feata 、featb
  2. 将前端构建产物放在 front/feat1 、front/feat2 中;将后端产物放在 backend/feata(端口 8001)、backend/featb(端口 8002)中并启动
  3. 页面上通过 cookie 指定前后端分支,比如需求 1 对应 front/feat1 & backend/feata
  4. 在 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 作为反向代理的目标机器吗?有没有相关资料或者案例可以模仿呢?

1610 次点击
所在节点    NGINX
5 条回复
hingbong
2021-11-30 14:47:27 +08:00
不用 lua 没试过,我们逻辑都是写在 lua 里
https://github.com/openresty/lua-resty-redis
https://github.com/cloudflare/lua-resty-cookie
用这两个应该就很容易实现了
superhack
2021-11-30 14:47:37 +08:00
可以。能写 lua 代码,什么都能控制。
NsLib
2021-11-30 14:53:46 +08:00
可以,这就是一个简单的泳道,可以去搜一下这个概念。
dnsjia
2021-11-30 16:53:05 +08:00
ffdshow233
2021-12-15 13:27:39 +08:00
用 lua ,可以写

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/819029

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX