Redirect user on 401 code

I have been trying to configure Kong to redirect users to /login page if the status code of any request is 401, but haven’t been able to do this.

I have made changes in kong_nginx.lua, here configured error page for 401.
We created a copy of the /kong_error_handler function and did some changes.
Can anyone provide suggestion on the recommended way to do this

Can I get some pointers for this questions

You don’t need to modify any Lua files, just create custom Nginx configuration:

See also: https://github.com/Kong/kong/issues/3349#issuecomment-376863443

Add there e.g.:

error_page 401 /error_401

location =/error_401 {
  internal;
  return 302 http://example.com/;
}

I didn’t test it, but I think you can find the idea from there.

Thanks for your inputs. I made changes as you have mentioned, used the below command to run kong:
kong start --nginx-conf custom_nginx.template

But still its not doing redirection as per the http status code.

I have deployed Kong using Vagrant on a Windows 10 machine.

You need to add enable this:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors

1 Like

We currently have multiple Nginx config files(one per app), can we append the contents of all these config files into one single custom Nginx config - is there a way to “include” them in more modular way into custom Nginx configuration file ?