access_by_lua without proxy_pass

43 views
Skip to first unread message

Gaurav Saxena

unread,
Jun 13, 2017, 2:44:43 AM6/13/17
to openresty-en
Hi All,

I am trying to setup a configuration where I am doing proxy_pass at one location. But, there are different locations that point at it. I want to add some validations before calling the particular location. I want to run basic check and then redirect the request to the main location. But, if I use rewrite, access_by_lua doesn't run. I did read about access_by_lua not being called for subrequests. But, if the first call directly hits a block with access_by_lua that should not be considered a subrequest. Is that understanding correct?

Example - 

server {
listen 4000;
location ~ ^/hello/world/(.*)$ {
set $path $1;

access_by_lua_block {
ngx.log (ngx.NOTICE, "in Lua")

ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
}

          rewrite ^ /$path last; # access_by_lua doesn't run
proxy_pass http://google.com?q=$channel; # access_by_lua does run
}

location ~ foo {
return 200 "bar";
}
}

curl localhost:4000/hello/world/foo 
200
bar

I would appreciate any insights here. 

bjoe2k4

unread,
Jun 13, 2017, 3:04:01 AM6/13/17
to openresty-en
rewrite runs before access phase, and since you rewrite to another location, you never reach the access phase for the first location. See: https://cloud.githubusercontent.com/assets/2137369/15272097/77d1c09e-1a37-11e6-97ef-d9767035fc3e.png

2 solutions:

1) use rewrite_by_lua and put your validation logic and rewrite logic there. no access_by_lua needed.
2) use access_by_lua and put your validation logic and rewrite logic there. no rewrite_by_lua needed.

Gaurav Saxena

unread,
Jun 13, 2017, 1:02:03 PM6/13/17
to openresty-en
Thanks a lot for the quick response and helpful information. I decided to go with 1. 
Reply all
Reply to author
Forward
0 new messages