openresty-lua-redis for simple grayscale publishing

1. The grayscale release topology map, the company's local access service enters the grayscale environment, and other accesses are originally produced.

 

 2. Configuration of nginx.conf

[root@VM_0_7_centos conf]# cat nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}

 
 
http {
include vhost/*.conf;
server {
        listen       22222;
        server_name  10.0.0.7 www.a.com localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
        default_type 'text/plain';
 
        location /test {
            content_by_lua_file /Users/chenguowei/local/openresty/nginx/lua_conf/huidu.lua;
        }
 
        location @client1 {
            proxy_pass http://client1;
        }
 
        location @client2 {
            proxy_pass http://client2;
        }
}
}

 

3.lugin script of nginx

When the source IP is myip, it enters grayscale, and the other enters the production environment.

[root@VM_0_7_centos ~]# cat   /Users/chenguowei/local/openresty/nginx/lua_conf/huidu.lua
local redis = require "resty.redis"
local cache = redis.new()
cache:set_timeout(60000)
 
local ok, err = cache.connect(cache, "10.0.0.205", 6379)
if not ok then
    ngx.say("failed to connect: redis", err)
    return
end
 
local local_ip = ngx.req.get_headers()["X-Real-IP"]
if== local_ip nil the then 
    local_ip = ngx.req.get_headers () [ " x_forwarded_for " ] 
End 
 
IF local_ip == nil the then 
    local_ip = NGX. var .remote_addr 
End 
 
 
local Intercept = Cache: GET ( "MyIP " ) 
 
IF Intercept == local_ip then 
    ngx.exec ( " @ client2 " )
     return 
end 
 
ngx.exec ( " @ client1 " )- no ngx.say () function can be executed before, otherwise the request will be wrong 
 
local ok, err= cache:close()
 
if not ok then
    ngx.say("failed to close: ", err)
    return
end

4. redis set my IP

[root@VM_0_42_centos src]# ./redis-cli -h 10.0.0.205 -p 6379
10.0.0.205:6379> set myip 10.0.0.7
OK
10.0.0.205:6379> 

 Reference:  https://blog.csdn.net/gochenguowei/article/details/85041578   

             https://www.jb51.net/article/114804.htm

Guess you like

Origin www.cnblogs.com/hixiaowei/p/12749023.html