Skip to content

Instantly share code, notes, and snippets.

@mjmeyer
Created February 17, 2016 22:58
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mjmeyer/6cf8d3c1199d028f0921 to your computer and use it in GitHub Desktop.
Save mjmeyer/6cf8d3c1199d028f0921 to your computer and use it in GitHub Desktop.
Http.cat erorr pages for nginx
# ---------- Status Cats Error Pages!!! via: https://http.cat/ ---------
#
# requires that a dns resolver be set for nginx as in: resolver 127.0.0.1;
# typically uses dnsmasq for 127.0.0.1 resolver
#
# Usage:
# place this file somewhere accessible to nginx. /etc/nginx/snippets is a decent choice.
# then inside the server block(s) you want cat themed error status responses do:
# include snippets/http-cat-error-pages.conf
#
recursive_error_pages on;
# all of the status defined at http.cat AND allowed by nginx to be error_page'd
#
error_page 300 301 302 303 304 305 307 400 401 402 403 404 405 406 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 425 426 429 431 444 450 451 500 501 502 503 506 507 508 509 599 /status-cats-error.html;
location /status-cats-error.html {
proxy_pass https://http.cat/$status;
}
# nginx wont let you use statuses < 300 in return stmts or in error_page stmts, so provide a
# proxied location to test those....
#
location ~ ^/test-status-cats/(?<cat>.*)$ {
proxy_pass https://http.cat/$cat/;
}
@mattusey
Copy link

Genius!

@mariansam
Copy link

Use the command tail -f /var/log/nginx/error.log to watch for errors when setting this up.

If you get a lot of SSL_do_handshake() failed errors and then one rewrite or internal redirection cycle while internally redirecting to "/status-cats-error.html" on every request, you need to add proxy_ssl_server_name on; to the http-cat-error-pages.conf config file. I added mine below line 12, so the file looks like this:

[...]

# Usage:
# place this file somewhere accessible to nginx. /etc/nginx/snippets is a decent choice.
# then inside the server block(s) you want cat themed error status responses do:
# include snippets/http-cat-error-pages.conf
#

recursive_error_pages on;
proxy_ssl_server_name on;  # https://stackoverflow.com/a/49116797/8483877

# all of the status defined at http.cat AND allowed by nginx to be error_page'd
#
error_page 300 301 302 303 304 305 307 400 401 402 403 404 405 406 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 425 426 429 431 444 450 451 500 501 502 503 506 507 508 509 599 /status-cats-error.html;

[...]

Source: https://stackoverflow.com/a/49116797/8483877

@mariansam
Copy link

And thanks for this, it's amazing ❤

@camprevail
Copy link

Use the command tail -f /var/log/nginx/error.log to watch for errors when setting this up.

If you get a lot of SSL_do_handshake() failed errors and then one rewrite or internal redirection cycle while internally redirecting to "/status-cats-error.html" on every request, you need to add proxy_ssl_server_name on; to the http-cat-error-pages.conf config file. I added mine below line 12, so the file looks like this:

[...]

# Usage:
# place this file somewhere accessible to nginx. /etc/nginx/snippets is a decent choice.
# then inside the server block(s) you want cat themed error status responses do:
# include snippets/http-cat-error-pages.conf
#

recursive_error_pages on;
proxy_ssl_server_name on;  # https://stackoverflow.com/a/49116797/8483877

# all of the status defined at http.cat AND allowed by nginx to be error_page'd
#
error_page 300 301 302 303 304 305 307 400 401 402 403 404 405 406 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 425 426 429 431 444 450 451 500 501 502 503 506 507 508 509 599 /status-cats-error.html;

[...]

Source: https://stackoverflow.com/a/49116797/8483877

Thanks, this fixed this exact issue I was having. Didn't used to happen until this year I think, I just never noticed it was broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment