Lua script does not close Nginx although ngx.close() defined
Asked Answered
T

1

2

I am running a docker with lua-nginx image.

In my Nginx conf file I call the lua script from server { } section:

server {
  listen 80;
  server_name _;

  location /payload {
      content_by_lua_file /etc/nginx/handler.lua;     
      proxy_pass <myUrl>;
  }
} 

I have an issue that no matter what, after the handler.lua script ends, it will go straight to proxy_pass. Even when lua script says ngx.close !!!

if method == "POST" then
   --do some stuff
else
   ngx.log(ngx.ERR, "wrong event request method: ", ngx.req.get_method())
   return ngx.exit (ngx.HTTP_NOT_ACCEPTABLE)
end

So when I do a GET request, after the "return ngx.exit()", the nginx.config will continue to the proxy_pass.

This makes my lua code meaningless. I want to have proxy_pass only if the method is POST.

Titillate answered 8/3, 2021 at 10:11 Comment(1)
Anybody suggestion?Titillate
F
0

Can you try access_by_lua_file which runs earlier in the process as in:

server {
  listen 80;
  server_name _;

  location /payload {
      access_by_lua_file /etc/nginx/handler.lua;     
      proxy_pass <myUrl>;
  }
} 

If that does not work you can try to proxy pass on a different location and you can call the proxy location inside your lua code:

server {
  listen 80;
  server_name _;

  location /payload {
      access_by_lua_file /etc/nginx/handler.lua;     
  } 
  location @proxy {
      proxy_pass <myUrl>;
  }

} 

And your lua code:

if method == "POST" then
   ngx.exec("@proxy")
else
   ngx.log(ngx.ERR, "wrong event request method: ", ngx.req.get_method())
   return ngx.exit (ngx.HTTP_NOT_ACCEPTABLE)
end
Flume answered 6/8 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.