you should be able to handle this in raw nginx by setting the 404 to a custom location, then issuing a return/rewrite in that location. the @ is commonly used because it won't match any legitimate traffic.
i haven't tried this with a 404 generated in openresty's lua blocks, but it should still work
error_page 404 = @foo;
location @foo {
return 301 /; # or rewrite
}