body_filter_by_lua does not work with echo_location

67 views
Skip to first unread message

Przemysław Sobala

unread,
Jan 25, 2023, 4:39:13 AM1/25/23
to openresty-en
Hello
The following configuration does not work - ngx.arg[1] is empty all the time and the "xxx" response body is returned to client. I'd expect the body_filter_by_lua to change the response body to "XXX".

server {
   server_name test.localhost;

   location /source {
       return 200 "xxx";
   }
   location / {
      echo_location /source;

      # update response body
      body_filter_by_lua_block {
         ngx.log(ngx.ERR, "[body_filter_by_lua_block] ngx.arg[1]: "..ngx.arg[1])
         ngx.arg[1] = string.upper(ngx.arg[1])
      }
   }

}

logged messages:
2023/01/25 10:16:51 [error] 8#8: *1 [lua] body_filter_by_lua:2: [body_filter_by_lua_block] ngx.arg[1]: , client: 127.0.0.1, server: test.localhost, request: "GET / HTTP/1.1", host: "test.localhost"
2023/01/25 10:16:51 [error] 8#8: *1 [lua] body_filter_by_lua:2: [body_filter_by_lua_block] ngx.arg[1]: , client: 127.0.0.1, server: test.localhost, request: "GET / HTTP/1.1", host: "test.localhost"

nginx version: 1.22.0
ngx_http_echo_module version: master from git
ngx_http_lua_module version: v0.10.19
--
best regards
Przemysław Sobala

Yichun Zhang

unread,
Jan 31, 2023, 7:47:10 PM1/31/23
to openre...@googlegroups.com
Hello!

The body_filter_by_lua_block directive does not work with merged nginx
subquests like in your example. Nginx subrequests are really painful.
You can avoid using subrequests in your configuration.

Best,
Yichun

Przemysław Sobala

unread,
Feb 6, 2023, 5:22:00 AM2/6/23
to openresty-en
Hello!
My configuration is the simplest one I could provide to show the problem. What I want to accomplish is to wrap "location /source" with "error_page" to switch upstream (not upstream's server) on 404:

location /source {
    proxy_pass http://upstream1;
    error_page 404 = @fallback;
}


location @fallback {
    proxy_pass http://upstream2;
}

and then filter the response body:

location / {
   echo_location /source;

   # update response body
   body_filter_by_lua_block {
      ngx.arg[1] = string.upper(ngx.arg[1])
   }
}

error_page directive works but body_filter_by_lua does not trigger.

Junlong Li

unread,
Feb 13, 2023, 2:02:21 AM2/13/23
to openresty-en
you can use the  proxy_next_upstream  proxy_next_upstream_triesand balancer_by_lua_block

here is an example of the balancer_by_lua_block 

Reply all
Reply to author
Forward
0 new messages