title: Nginx lua openresty variable scope
body: <p>Here is working example of openresty nginx.conf file. In this example I make request to redis multiple times. As you can see I request data from redis first to check if domain can get SSL then again to get what backend to proxy request to, and then I've added S3 proxy and I would need to request data from redis again. I'm new to OpenResty and Lua and I wonder if it is possible to fetch data from redis once and use it multiple times across the script?</p>
  <pre><code>user www-data;
  worker_processes  auto;
  pid /run/openresty.pid;
  
  events {
      worker_connections  1024;
  }
  
  error_log /var/log/openresty/error.log debug;
  
  http {
      resolver 127.0.0.53 ipv6=off;
      lua_shared_dict acme 16m;
  
      init_by_lua_block {
          require(&quot;resty.acme.autossl&quot;).init({
              tos_accepted = true,
              staging = true,
              account_key_path = &quot;/etc/openresty/account.key&quot;,
              account_email = &quot;didnt@forgot.removing&quot;,
              domain_whitelist_callback = function(domain)
                  local redis = require &quot;resty.redis&quot;
                  local rds = redis:new()
  
                  local ok, err = rds:connect(&quot;127.0.0.1&quot;, 6379)
                  if not ok then
                      ngx.log(ngx.ERR, &quot;failed to connect to redis: &quot;, err)
                      return ngx.exit(500)
                  end
  
                  local res, err = rds:exists(domain)
  
                  if res == 1 then
                      return true
                  end
                  if res == 0 then
                      return false
                  end
              end
          })
      }
  
      init_worker_by_lua_block {
          require(&quot;resty.acme.autossl&quot;).init_worker()
      }
  
      server {
          access_log /var/log/openresty/access.log;
  
          listen 80;
          listen 443 ssl;
          server_name _;
  
          location / {
              set $backend '';
              set $tenant '';
  
              access_by_lua '
                  local domain = ngx.req.get_headers()[&quot;Host&quot;]
                  local key = &quot;site:&quot; .. domain
  
                  if not domain then
                  ngx.log(ngx.ERR, &quot;message 404 missing&quot;)
                  return ngx.exit(404)
                  end
  
                  local redis =  require &quot;resty.redis&quot;
                  local rds = redis:new()
  
                  local ok, err = rds:connect(&quot;127.0.0.1&quot;, 6379)
                  if not ok then
                      ngx.log(ngx.ERR, &quot;failed to connect to redis: &quot;, err)
                      return ngx.exit(500)
                  end
  
                  local all, err = rds:hgetall(key)
                  if not all then
                      ngx.log(ngx.ERR, &quot;no komprende: &quot;, err)
                      return ngx.exit(505)
                  end
  
                  if all == ngx.null then
                      ngx.log(ngx.ERR, &quot;no host found for key &quot;, key)
                      return ngx.exit(404)
                  end
  
                  local result = {}
                      for i = 1, #all, 2 do
                      result[all[i]] = all[i+1]
                  end
  
                  ngx.var.backend = result[&quot;backend&quot;]
                  ngx.var.tenant = result[&quot;tenantID&quot;]
  
                  ngx.log(ngx.ERR, &quot;uhm: &quot;, ngx.var.backend)
              ';
  
              add_header X-TenantID $tenant always;
              proxy_pass http://$backend;
  
          }
  
          location ~* ^/static/(.*) {
              resolver               127.0.0.53 valid=300s;
              resolver_timeout       10s;
              set $s3_bucket        'drasha.ams3.digitaloceanspaces.com';
              set $url_full         '$1';
              proxy_http_version     1.1;
              proxy_set_header       Host $s3_bucket;
              proxy_set_header       Authorization '';
              proxy_hide_header      x-amz-id-2;
              proxy_hide_header      x-amz-request-id;
              proxy_hide_header      Set-Cookie;
              proxy_ignore_headers   &quot;Set-Cookie&quot;;
              proxy_buffering        off;
              proxy_intercept_errors on;
              proxy_pass             http://$s3_bucket/AYAYA/$url_full;
          }
  
          lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
          lua_ssl_verify_depth 2;
          ssl_certificate /etc/openresty/default.pem;
          ssl_certificate_key /etc/openresty/default.key;
          ssl_certificate_by_lua_block {
              require(&quot;resty.acme.autossl&quot;).ssl_certificate()
          }
  
          location /.well-known {
              content_by_lua_block {
                  require(&quot;resty.acme.autossl&quot;).serve_http_challenge()
              }
          }
      }
  }
  </code></pre>