daniel
daniel

Reputation: 47

How to get lua variables back into nginx variables

I have a lua script that uses lua-resty to call another service via co-sockets. Now I would like to use the information from this call to route the request in nginx.

nginx includes the lua script in access_by_lua* which sets the var like this:

ngx.var.blocked = '1'

and routes in the location like this:

if ( $blocked ) {
     proxy_pass   http://haproxy-9001;
     break;
}

the problem is now that nginx does not pick up the variable change (in this phase). if I include the lua script in set_by_lua* phase then the variable passing works but I dont have access to the co-sockets in this phase.

Any idea how to get the variable out of lua land into the nginx variable in the access_by_lua, rewrite_by_lua or content_by_lua phase so that I can use the co-socket api to make a http call?

Upvotes: 2

Views: 5322

Answers (2)

Alexander Altshuler
Alexander Altshuler

Reputation: 3064

if nginx directive is implemented by https://nginx.ru/en/docs/http/ngx_http_rewrite_module.html.

Obviously it works on rewrite phase, so your changes at access phase doesn't work. Just don't use if. Below is snippet from one of my nginx config:

proxy_pass $scheme://$upstream$uri$is_args$args;

Just set $upstream variable at access phase and it will work at content phase (proxy_pass).

Upvotes: 2

Roman Kushin
Roman Kushin

Reputation: 65

Maybe you could capture location with that proxy instead of variable, it works in access_by_lua scope https://github.com/openresty/lua-nginx-module#ngxlocationcapture

Upvotes: 0

Related Questions