1

I want to define a variable and use it in the location block in OpenResty config file. Variable is defined same follow:

location /projects {
   set $my_var '';
   content_by_lua_block {
      ngx.var.my_var = "h@3265";
   }

   header_filter_by_lua '
      local val = ngx.header["x-ausername"]
      if val then
         if (val ~= "sample3")
         and (val ~= ngx.var.my_var) -- this variable does not work
         and (val ~= "sample2")
         and (val ~= "sample1")
         and (val ~= "anonymous") then
            return ngx.exit(400)
         end
      end
   ';

   proxy_pass        http://MYSERVER.LOCAL:6565;
   proxy_set_header Host $host:$server_port;
   access_log off;
}

But doesn't pars ngx.var.my_var. How can I define a variable and use it in any part of nginx.conf file?

2
  • I'm confused; you're using proxy_pass and content_by_lua at the same time, what do you want to achieve? Commented Jan 29, 2020 at 10:57
  • I wanna at first checked x-ausername in request header if that equal with my variable then that username permitted to login to my website. Commented Jan 30, 2020 at 6:13

2 Answers 2

1

If you just need to set a const value to your variable - just use set $my_var 'h@3265'; directive and avoid content_by_lua_block.

It is not possible to use proxy_pass and content_by_lua_block in the same place because both are content phase directives. content_by_lua_block just ignored in your config.

If you need to use more complex Lua logic to set the variable - use set_by_lua_block

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you all, I changed config file into follow and that works fine.

location /projects {

   header_filter_by_lua '
      local my_var = "h%403265"             #**Note**
      local val = ngx.header["x-ausername"]
      if val then
         if (val ~= "sample3")
         and (val ~= my_var) 
         and (val ~= "sample2")
         and (val ~= "sample1")
         and (val ~= "anonymous") then
            return ngx.exit(400)
         end
      end
   ';

   proxy_pass        http://MYSERVER.LOCAL:6565;
   proxy_set_header Host $host:$server_port;
   access_log off;
}

Note: in config file for accept @ in a variable should used to percent encoding. So,@ equal to %40 (h@3265 --> h%403265).

5 Comments

The right place for acces controll is the aptly named access_by_lua_block, not header_filter_by_lua which only runs much later.
I checked access_by_lua_block but it can control through request's IP. but I need only checked x-ausername in request header and then if that equal with my variable then that username permitted to login to my website.
The problem is that header_filter_by_lua runs at the end of the content phase, so you're wasting resources connecting to the proxy only to then throw the entire response away.
To clarify: header_filter is for filtering response headers before they get sent to the client, not to filter the request headers before handling the request.
You're right and thank you for your attention. But I wanna control on users based on username and while a user fill username and password fields I couldn't check it until authentication process completed. So as you said I should filter it on response header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.