We use the ngx_http_auth_request_module to authenticate requests by calling one of our services. Unfortunately, if the request is rejected, it returns the default 401 error page:
<html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.9.10</center> </body> </html>
Which is a bit out of place, when the rest of our responses are JSON. A simple fix is to override it:
server {
...
error_page 401 @401_json;
location @401_json {
default_type application/json;
return 401 '{"error":{"message":"Unauthorised"}}';
}
}
But remember this will be used for all 401s, not just those from the auth module.