Nginx - reads a value from redis and stores it in a variable

Is there a way to read the redis value and save it in the nginx variable?

I want to use it for a multi-domain website where subdomains will point to different IP addresses. All subdomains will be saved in redis as follows:

"subdomain" => "address_for_proxy_pass" 

So, I need to parse the subdomain (make), save it in a variable (done) and make a request redis (done) and save the result in a variable. How to do it easily?

+4
source share
2 answers

lua-resty-redis cannot be used in set_by_lua - https://github.com/openresty/lua-resty-redis#limitations

But $foobar can be installed directly in lua via ngx.var.foobar using access_by_lua . Details are available here: http://openresty.org/#DynamicRoutingBasedOnRedis (thanks to OP and comments).

The variable must be initialized - set $myvar ''

  # initialize variable set $backend ''; access_by_lua ' local redis = require "resty.redis" local red = redis:new() -- these ENV vars must be initialized with `env` keyword red:connect(os.getenv("REDIS_PORT_6379_TCP_ADDR"), os.getenv("REDIS_PORT_6379_TCP_PORT")) local res, err = red:get(ngx.var.appid) -- set the variable directly ngx.var.backend = res '; proxy_pass http://$backend; 
0
source

All Articles