Forum: NGINX Return JSON for 404 error instead of html

2974d09ac2541e892966b762aad84943?d=identicon&s=25 justink101 (Guest)
on 2014-05-13 06:09
(Received via mailing list)
How can I return a custom JSON body on 404, instead of the default html
of:

<html>

    <head>
        <title>404 Not Found</title>
    </head>

    <body bgcolor="white">
        <center>
            <h1>404 Not Found</h1>
        </center>
        <hr>
        <center>nginx</center>
    </body>

</html>

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,250033,250033#msg-250033
Ba5061f1803f9ab0cfc3d53366d49546?d=identicon&s=25 Lord Nynex (Guest)
on 2014-05-13 08:42
(Received via mailing list)
Justink101,

Using the echo module

error_page 404 @404;
location @404 { echo '{"status": "Not Found"}'; }
A8a14adc688e0d5ed2110a41bbad909e?d=identicon&s=25 Matthias Rieber (Guest)
on 2014-05-13 10:44
(Received via mailing list)
On Mon, 12 May 2014, Lord Nynex wrote:

> Justink101,
>
> Using the echo module
>
> error_page 404 @404;
> location @404 { echo '{"status": "Not Found"}'; }

if your using proxy_pass, don't forget:

proxy_intercept_errors on;

Matthias
40b4c848b8fcd63b0cb60b9d170c3a77?d=identicon&s=25 Valentin V. Bartenev (Guest)
on 2014-05-13 11:01
(Received via mailing list)
On Monday 12 May 2014 23:42:03 Lord Nynex wrote:
> Justink101,
>
> Using the echo module
>
> error_page 404 @404;
> location @404 { echo '{"status": "Not Found"}'; }
>
[..]

Instead of using 3rd-party echo module, you can utilize the return
directive
for the same purpose:

  return 200 '{"status": "Not Found"}';

Reference: http://nginx.org/r/return

  wbr, Valentin V. Bartenev
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2014-05-13 15:32
(Received via mailing list)
>
​I would have intuitively written code 404 rather than 200 on this one
since the aim is to send a 404 error answer.​
Am I wrong? Would that loop?
​
---
*B. R.*
40b4c848b8fcd63b0cb60b9d170c3a77?d=identicon&s=25 Valentin V. Bartenev (Guest)
on 2014-05-13 16:37
(Received via mailing list)
On Tuesday 13 May 2014 15:30:56 B.R. wrote:
> ​I would have intuitively written code 404 rather than 200 on this one
> since the aim is to send a 404 error answer.​
> Am I wrong? Would that loop?
>

I wrote an equivalent of "echo".  The logic is that in this handler we
provide
the page for 404 which actually exists.

   wbr, Valentin V. Bartenev
21a46ad06bed3b803d41cb2b8d6b1433?d=identicon&s=25 Justin Dorfman (Guest)
on 2014-05-13 17:49
(Received via mailing list)
Out of curiosity, would the mime/content type show up as
application/json
or text/plain?


Regards,

Justin Dorfman <http://www.twitter.com/jdorfman>

Director of Developer Relations
MaxCDN <http://twitter.com/MaxCDNDeveloper>

Email / IM: jdorfman@maxcdn.com
Mobile: 818.485.1458
Twitter: @jdorfman <http://www.twitter.com/jdorfman>
40b4c848b8fcd63b0cb60b9d170c3a77?d=identicon&s=25 Valentin V. Bartenev (Guest)
on 2014-05-13 17:54
(Received via mailing list)
On Tuesday 13 May 2014 08:48:50 Justin Dorfman wrote:
> Out of curiosity, would the mime/content type show up as application/json
> or text/plain?
>

It depends on the default_type directive:
http://nginx.org/r/default_type

  wbr, Valentin V. Bartenev
21a46ad06bed3b803d41cb2b8d6b1433?d=identicon&s=25 Justin Dorfman (Guest)
on 2014-05-13 18:07
(Received via mailing list)
application/octet-stream it is =p


Regards,

Justin Dorfman <http://www.twitter.com/jdorfman>

Director of Developer Relations
MaxCDN <http://twitter.com/MaxCDNDeveloper>

Email / IM: jdorfman@maxcdn.com
Mobile: 818.485.1458
Twitter: @jdorfman <http://www.twitter.com/jdorfman>
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2014-05-13 21:16
(Received via mailing list)
I understand the logic, but when using that handler through error_page
404
@404, won't the handler's 200 status overload the original 404 one?

---
*B. R.*
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2014-05-13 22:14
(Received via mailing list)
On Tue, May 13, 2014 at 09:15:16PM +0200, B.R. wrote:
> I understand the logic, but when using that handler through error_page 404
> @404, won't the handler's 200 status overload the original 404 one?

http://nginx.org/r/error_page indicates that it won't unless "=" is
used.

  f
--
Francis Daly        francis@daoine.org
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2014-05-14 02:53
(Received via mailing list)
Many thanks to both of you!
Another step in deeper understanding on how to use nginx. :o)
---
*B. R.*
2974d09ac2541e892966b762aad84943?d=identicon&s=25 justink101 (Guest)
on 2014-05-14 23:27
(Received via mailing list)
Thanks for the replies and sorry about the delay in responding. This is
what
we ended up using:

  error_page 404 = @four_o_four;

  location @four_o_four {
    internal;
    more_set_headers "X-Host: web4.ourdomain.com";
    more_set_headers "Content-Type: application/json";
    return 404 '{"status":"Not Found"}';
  }

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,250033,250095#msg-250095
2974d09ac2541e892966b762aad84943?d=identicon&s=25 justink101 (Guest)
on 2014-05-15 00:39
(Received via mailing list)
Noticed that the proxy request response headers are being thrown away in
our
404 block. Note that the proxied request is returning 404. If I try and
fetch a header that I know is being returned from the proxy it is
undefined.

location @four_o_four {
    internal;
    more_set_headers "X-Host: $sent_http_x_host";
    return 404 '{
    "error": {
  "status_code": 404,
        "status": "Not Found",
        "message": "The requested resource does not exist."
    }
}';
  }

In our block example,x-host is being returned from the proxy response,
but
not visible in the @four_o_four location block. Any idea why?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,250033,250096#msg-250096
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2014-05-15 09:18
(Received via mailing list)
On Wed, May 14, 2014 at 06:38:45PM -0400, justink101 wrote:

Hi there,

> If I try and
> fetch a header that I know is being returned from the proxy it is
> undefined.
>
> location @four_o_four {
>     internal;
>     more_set_headers "X-Host: $sent_http_x_host";

What value do you think $sent_http_x_host has, and why do you think
that?

Compare $http_name and $sent_http_name in
http://nginx.org/en/docs/http/ngx_http_core_module...
with $upstream_http_... in
http://nginx.org/en/docs/http/ngx_http_upstream_mo...

and then use $upstream_http_x_host.

  f
--
Francis Daly        francis@daoine.org
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.