Catch / mask 404

24 views
Skip to first unread message

ores...@yalis.fr

unread,
Oct 3, 2017, 3:32:31 AM10/3/17
to openre...@googlegroups.com
Hi!

Is it possible, in ?_by_lua_file, when the regular request handled by
Nginx results in 404 Not Found, to keep this from being sent to the
browser, and redirect to another page instead (eg. a login page)?

I've seen several articles on the Internet on how to generate a 404, but I
have not seen how to keep the 404 from happening...
Thanks,

Yves.

tokers

unread,
Oct 3, 2017, 6:08:38 AM10/3/17
to openresty-en
Maybe rewrite the status code from 404 to 302 and set the Location response header in the header_filter_by_lua phase can help you?
like:
header_filter_by_lua_block {
   if ngx.status == 404 then
     ngx.status = 302
     ngx
.header["Location"] = location_url
  end
}



jona...@findmeon.com

unread,
Oct 3, 2017, 4:47:01 PM10/3/17
to openresty-en
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
}
Reply all
Reply to author
Forward
0 new messages