Monday, April 23, 2012

NGINX: Create Custom 404 / 403 Error Page

How do I create a custom static HTTP 404 or HTTP 403 error page under nginx web server?

First create 404.html in your document root. The default is location is /usr/local/nginx/html/. So create a HTML file as follows:
# vi /usr/local/nginx/html/404.html
Sample outputs:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Error 404 Not Found</title>
<style>
<!--
body {font-family: arial,sans-serif}
img { border:none; }
//-->
</style>
 
</head>
<body>
<blockquote>
<h2>Error 404 Not Found</h2>
 
Our apologies for the temporary inconvenience. The requested URL was not found on this server. We suggest you try one of the links below:
<ul>
<li><b>Verify url and typos</b> - The web page you were attempting to view may not exist or may have moved - try <em>checking the web address for typos</em>.<//li>
<li><b>E-mail us</b> - If you followed a link from somewhere, please let us know at <a href="mailto:webmaster@example.com">webmaster@example.com</a>. Tell us where you came from and what you were looking for, and we'll do our best to fix it.</li>
</ul>
</blockquote>
 
</body>
</html>
 
Edit /usr/local/nginx/conf/nginx.conf file, enter:
# vi /usr/local/nginx/conf/nginx.conf
Append / edit config as follows:
 
error_page 404 = /404.html;
 
An example to prevent clients fetching error pages directly:
error_page 404 /404.html;
location /404.html {
internal;
}
An example of HTTP 403 error page:
 
error_page 403 /403.html;
location = /403.html {
root html;
allow all;
}
 
Save and close the file. Reload nginx, enter:
# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload

No comments:

Post a Comment