V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
vevlins
V2EX  ›  NGINX

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

  •  
  •   vevlins · 2021-11-30 14:44:11 +08:00 · 1559 次点击
    这是一个创建于 850 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前提:只听过没用过,对 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 作为反向代理的目标机器吗?有没有相关资料或者案例可以模仿呢?

    5 条回复    2021-12-15 13:27:39 +08:00
    hingbong
        1
    hingbong  
       2021-11-30 14:47:27 +08:00   ❤️ 1
    不用 lua 没试过,我们逻辑都是写在 lua 里
    https://github.com/openresty/lua-resty-redis
    https://github.com/cloudflare/lua-resty-cookie
    用这两个应该就很容易实现了
    superhack
        2
    superhack  
       2021-11-30 14:47:37 +08:00   ❤️ 1
    可以。能写 lua 代码,什么都能控制。
    NsLib
        3
    NsLib  
       2021-11-30 14:53:46 +08:00   ❤️ 1
    可以,这就是一个简单的泳道,可以去搜一下这个概念。
    dnsjia
        4
    dnsjia  
       2021-11-30 16:53:05 +08:00
    ffdshow233
        5
    ffdshow233  
       2021-12-15 13:27:39 +08:00 via Android
    用 lua ,可以写
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2889 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 11:21 · PVG 19:21 · LAX 04:21 · JFK 07:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.