美文网首页
Windows环境下Nginx+Lua+Redis实现灰度发布

Windows环境下Nginx+Lua+Redis实现灰度发布

作者: 惜时流光沿途留殇 | 来源:发表于2020-09-24 12:56 被阅读0次

    Windows环境下Nginx+Lua+Redis实现灰度发布

    1.配置nginx.conf文件

    
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
        
        upstream new_version {
            server 127.0.0.1:8080;
        }
        upstream old_version {
            server 127.0.0.1:9090;
        }
    
        server {
          listen       80;
          server_name  localhost;
          
          ####################【lua处理】####################
          ## 1、将对localhost访问由/opt/app/lua/dep.lua进行处理
          ## 2、根据逻辑处理后,决定回调如下两个其中1个内部跳转
          location / 
          {
            default_type "text/html"; 
            content_by_lua_file D:/sortware/openresty/openresty-1.17.8.2-win64/conf/dep.lua; # 指定由lua文件处理http请求
            #add_after_body "$http_x_forwarded_for";
          }
    
          ################【nginx内部跳转1】#################
          # 2.0版本的请求转发
          location @new_version
          {
            proxy_pass http://new_version; # 请求转发到2.0版本api的服务器A
            proxy_set_header Host $http_host;
          }
    
          ################【nginx内部跳转2】#################
          # 1.11版本的请求转发
          location @old_version
          {
            proxy_pass http://old_version; # 请求转发到1.11版本api的服务器B
            proxy_set_header Host $http_host;
          }
    
    
          error_page   500 502 503 504 404  /50x.html;
          location = /50x.html {
            root   /usr/share/nginx/html;
          }
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    
    

    2.lua脚本

    -- 1. 获取客户端请求的ip网络地址
    clientIP = ngx.req.get_headers()["X-Real-IP"]
    if clientIP == nil then
      clientIP = ngx.req.get_headers()["x_forwarded_for"]
    end
    if clientIP == nil then
      clientIP = ngx.var.remote_addr
    end
    
    -- 2. 连接到的redis数据库服务
    local redis = require "resty.redis"
    local red = redis:new()
    red:set_timeout(1000)
    local ok, err = red:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    else
    --  ngx.say("redis connetc success")
    end
    
    -- 3. 从redis数据库查询ip,是否存在对应的记录
    local res, flags, err = red:get(clientIP)
    -- ngx.say("value key: ",res,clientIP)
    if err then
      ngx.say("failed to get clientIP ", err)
      return
    end
    
    -- 4. 如果memcached数据库存在对应ip记录,则新的后台api版本对应的服务器url
    if res == "1" then
      ngx.exec("@new_version") 
      return
    end
    
    -- 5. 反之走老的后台api对应的服务器url
    ngx.exec("@old_version")
    return 
    
    • 检查后发现,前面function中已经有ngx.say的过程,所以导致此问题。注释掉rediect前的所有say操作和print操作后即可

    相关文章

      网友评论

          本文标题:Windows环境下Nginx+Lua+Redis实现灰度发布

          本文链接:https://www.haomeiwen.com/subject/wnpzyktx.html