Last active
March 29, 2023 13:05
-
-
Save kerns/32e281097d4787dcb3e9fddecd2d6e22 to your computer and use it in GitHub Desktop.
One NGINX Error Page to Rule Them All
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In /etc/nginx/ I have a file called errors.conf, which should try and pass errors to a error.php file in /var/www/admin/public | |
---- | |
# Error handler, codes can also be combined | |
error_page 401 /error.php?c=401; | |
error_page 403 /error.php?c=403; | |
error_page 404 /error.php?c=404; | |
error_page 500 /error.php?c=500; | |
error_page 502 503 504 /error.php?c=50x; | |
# Serve error.php from www/admin/public | |
location = /error.php { | |
root /var/www/admin/public; | |
include fastcgi.conf; | |
auth_basic off; | |
} | |
---- | |
errors.conf is included in every server block, which would typically look something like this. | |
---- | |
server { | |
listen 80; | |
server_name demosite.domain.com; | |
root /var/www/demosite/public; | |
access_log /var/www/demosite/logs/demosite.domain.com.access.log; | |
error_log /var/www/demosite/logs/demosite.domain.com.error.log; | |
location = /favicon.ico { | |
return 204; | |
access_log off; | |
log_not_found off; | |
} | |
location / { | |
index index.html index.htm index.php; | |
} | |
include errors.conf; | |
location ^~ /public/errors { | |
auth_basic off; | |
allow all; | |
} | |
# Pass the PHP scripts to FastCGI server listening on the php-fpm socket | |
location ~ \.php$ { | |
try_files $uri =404; | |
fastcgi_pass unix:/run/php/php7.0-fpm.sock; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
} | |
Lastly, error.php looks like this. | |
---- | |
<!DOCTYPE html> | |
<?php | |
// Map error code to message string | |
$_err = Array("401" => "Authorization Required", | |
"403" => "Access Restricted", | |
"404" => "File Not Found", | |
"500" => "Some Sort of Server Error", | |
"50x" => "Temporarily Unavailable"); | |
// Get the error string | |
function errorString($error) | |
{ | |
global $_err; | |
foreach ($_err as $key => $val) { | |
if ($key == $error) | |
return $val; | |
} | |
return "Unknown Error"; | |
} | |
isset($_GET['c']) or die("Unknown Error"); | |
$error = $_GET['c']; | |
$str = errorString($error); | |
?> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title><?= $error ?> - <?= $str ?></title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link href='https://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.css"> | |
<style type="text/css"> | |
@font-face { | |
font-family: 'icomoon'; | |
src: url("data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMg8SBSoAAAC8AAAAYGNtYXAXVtKHAAABHAAAAFRnYXNwAAAAEAAAAXAAAAAIZ2x5Ziqfw4EAAAF4AAAAoGhlYWQIzNoOAAACGAAAADZoaGVhB4IDxgAAAlAAAAAkaG10eAoAAFMAAAJ0AAAAFGxvY2EAKABkAAACiAAAAAxtYXhwAAcAIAAAApQAAAAgbmFtZZlKCfsAAAK0AAABhnBvc3QAAwAAAAAEPAAAACAAAwMAAZAABQAAApkCzAAAAI8CmQLMAAAB6wAzAQkAAAAAAAAAAAAAAAAAAAABEAAAAAAAAAAAAAAAAAAAAABAAADpAAPA/8AAQAPAAEAAAAABAAAAAAAAAAAAAAAgAAAAAAADAAAAAwAAABwAAQADAAAAHAADAAEAAAAcAAQAOAAAAAoACAACAAIAAQAg6QD//f//AAAAAAAg6QD//f//AAH/4xcEAAMAAQAAAAAAAAAAAAAAAQAB//8ADwABAAAAAAAAAAAAAgAANzkBAAAAAAEAAAAAAAAAAAACAAA3OQEAAAAAAQAAAAAAAAAAAAIAADc5AQAAAAABAFMAUwPAAy0AHQAAJQEmNDcBNjIXFhQPASEyFhUUBiMhFx4BFRQGBwYiAZP+wBMTAUASNhITE9ICZRslJRv9m9IKCQkKEjZTAUASNhIBQBMTEjYS0yUbGyXTCRgMDBgJEwAAAQAAAAEAADSPFpdfDzz1AAsEAAAAAADS90ryAAAAANL3SvIAAAAAA8ADLQAAAAgAAgAAAAAAAAABAAADwP/AAAAEAAAAAAADwAABAAAAAAAAAAAAAAAAAAAABQQAAAAAAAAAAAAAAAIAAAAEAABTAAAAAAAKABQAHgBQAAEAAAAFAB4AAQAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAOAK4AAQAAAAAAAQAHAAAAAQAAAAAAAgAHAGAAAQAAAAAAAwAHADYAAQAAAAAABAAHAHUAAQAAAAAABQALABUAAQAAAAAABgAHAEsAAQAAAAAACgAaAIoAAwABBAkAAQAOAAcAAwABBAkAAgAOAGcAAwABBAkAAwAOAD0AAwABBAkABAAOAHwAAwABBAkABQAWACAAAwABBAkABgAOAFIAAwABBAkACgA0AKRpY29tb29uAGkAYwBvAG0AbwBvAG5WZXJzaW9uIDEuMABWAGUAcgBzAGkAbwBuACAAMQAuADBpY29tb29uAGkAYwBvAG0AbwBvAG5pY29tb29uAGkAYwBvAG0AbwBvAG5SZWd1bGFyAFIAZQBnAHUAbABhAHJpY29tb29uAGkAYwBvAG0AbwBvAG5Gb250IGdlbmVyYXRlZCBieSBJY29Nb29uLgBGAG8AbgB0ACAAZwBlAG4AZQByAGEAdABlAGQAIABiAHkAIABJAGMAbwBNAG8AbwBuAC4AAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") format("truetype"); | |
font-weight: normal; | |
font-style: normal; | |
} | |
.icon-arrow-left:before { | |
content: "\e900"; | |
font-family: 'icomoon' !important; | |
speak: none; | |
font-style: normal; | |
font-weight: normal; | |
font-variant: normal; | |
text-transform: none; | |
line-height: 1; | |
/* Better Font Rendering =========== */ | |
-webkit-font-smoothing: antialiased; | |
-moz-osx-font-smoothing: grayscale; | |
} | |
:root { | |
font-size: calc(16px + 1vw); | |
} | |
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) { | |
:root { | |
font-size: calc(11px + 1vw); | |
} | |
} | |
html { | |
box-sizing: border-box; | |
font: 1em/1.5 'Lato', sans-serif; | |
min-height: 100%; | |
/*box-shadow: inset 0 0 15vw #171a1c;*/ | |
} | |
body { | |
background-color: #25292C; | |
font-family: 'Lato', 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
color: #efefef; | |
} | |
main { | |
padding: 2em; | |
max-width: 800px; | |
margin: auto; | |
} | |
p, h1, h2, h3 { | |
color: #dadada; | |
} | |
h1 { | |
color: #383D40; | |
font-size: 2.25em; | |
line-height: 1; | |
margin-bottom: 1em; | |
font-weight: normal; | |
font-style: italic; | |
} | |
h1 em { | |
color: #ffff4d; | |
display: block; | |
font-weight: 300; | |
font-style: normal; | |
} | |
h1 em span { | |
display: none; | |
} | |
h3 { | |
font-size: 1.25em; | |
line-height: 1.2; | |
font-weight: 300; | |
margin-bottom: 2em; | |
} | |
h3 b { | |
color: white; | |
display: block; | |
} | |
hr { | |
border: 0; | |
border-bottom: 1px solid #FFF; | |
border-top: 1px solid #BBB; | |
} | |
p { | |
color: #999; | |
} | |
p a { | |
color: #99ffe6; | |
text-decoration: none; | |
display: inline-block; | |
-webkit-transition: color .2s ease-out; | |
transition: color .2s ease-out; | |
} | |
p a :link { | |
color: #99ffe6; | |
-webkit-transition: color .2s ease-out; | |
transition: color .2s ease-out; | |
} | |
p a:visited { | |
color: #99ffe6; | |
} | |
p a:hover { | |
color: #00b386; | |
-webkit-transition: color .2s ease-out; | |
transition: color .2s ease-out; | |
} | |
p a:active { | |
color: #99ffe6; | |
-webkit-transition: none; | |
transition: none; | |
} | |
.btn { | |
border: 0 none; | |
color: white; | |
cursor: pointer; | |
border-radius: 1px; | |
display: inline-block; | |
margin: 0; | |
outline: 0 none; | |
text-decoration: none; | |
line-height: inherit; | |
padding: 0.675em .725em; | |
zoom: 1; | |
-webkit-transition: background-color .2s ease-out; | |
transition: background-color .2s ease-out; | |
} | |
.btn:hover { | |
color: white; | |
} | |
.back.btn { | |
background: #00cc99; | |
font-family: Lato, sans-serif | |
} | |
.back.btn:hover { | |
background: #00b386; | |
} | |
.btn:before { | |
line-height: .75; | |
display: inline-block; | |
margin-right: .25em; | |
} | |
</style> | |
</head> | |
<body> | |
<main> | |
<h1><?= $error ?> <em><span>—</span> <?= $str ?></em></h1> | |
<h3>Something went wrong. <b>Horribly wrong.</b></h3> | |
<a class="btn icon-arrow-left back" href="javascript:window.history.back()">Go Back</a> | |
<p>Or visit the <a href="/">front page</a> of this site.<br/>If it's urgent, <a href="your_mailto:">contact</a> the management.</p> | |
</main> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment