Nginx is a free and open source web server. It also act as a reverse proxy, caching and load balancer server. This tutorial shows, how to configure Nginx to use custom error pages for 404/403 on Linux or Unix-like system.
How to configure Nginx to use custom 404 error page
First create a 404.html in your document root. The default is location is /usr/local/nginx/html/ or /var/www/htm/. So create a HTML file as follows:
# vi /usr/local/nginx/html/404.html
OR
# vi /var/www/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> <p>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>
How to configuring Nginx to use my 404.html file as error page
Edit /usr/local/nginx/conf/nginx.conf file, enter:
# vi /usr/local/nginx/conf/nginx.conf
OR
# vi /etc/nginx/sites-available/default
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; internal; }
The error_page can be placed in only http, server and location directives. Save and close the file. Reload the nginx server, enter:
# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
OR
# nginx -t && nginx -s reload
OR
$ sudo systemctl reload nginx
Here is a sample error page from my site when visited (your-domain/404dfoo):
Fig.01: Sample custom error 404 page in Nginx
How do I redirect to a specific URL for error processing?
Yes, URL redirects for error processing is possible as follows:
# # Redirect to forbidden.php for HTTP status code 403 # error_page 403 https://www.cyberciti.biz/error/forbidden.php; # # Redirect to notfound.pl for HTTP status code 404 # with redirect status codes to 301 # error_page 404 =301 https://www.cyberciti.biz/cgi-bin/notfound.pl;
A list of common error code
- HTTP 404 – Web page not found on this server.
- HTTP 403 – The server is configured to deny access to your request. Usually login urls such as /wp-admin/ or /phpmysqladmin/ are blocked using this method.
- HTTP 500/502/503/504 – Internal server problem such as miss configured server or back-end down.
For more info see Nginx documentation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 12 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
why in /usr/local?
I always pack this brilliant piece of software for OS I use.
Thanks for the tutorial. I need dynamic 404 page with php example 404.php but looks like nginx ignore php file for 404 page.
Any help realy appreciate.
Thanks, but this is a highly unclear posting. Where do those lines go — inside the “http” directive, or inside each server block?
Try any one of the following context:
Thanks Vivek. Can I do outside http?
And what do the permissions of the files have to be — chmod 777?
644
files should always have 644 and directories 755
Doesn’t work inside http context, and doesn’t work outside it.
Shows me this error:
Is this only allowed inside location blocks? I’m using 1.0.8.
Here is another example put it in server context:
Firstly, that’s a highly inefficient nginx conf file with all those IF conditions.
Anyway, my error_pages blocks are exactly the same, except that I want the error pages across my site — at least the default ones — to be the same. So I have this:
error_page 404 /404.html;
location = /404.html {
root /etc/nginx/html;
internal;
}
error_page 403 /403.html;
location = /403.html {
root /etc/nginx/html;
allow all;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /etc/nginx/html;
}
Where /etc/nginx/html is CHMOD 777.
Not working.
Any ideas?
Phoenix, What you posted worked perfect! And the files don’t need to be 777.
server {
error_page 403 = /403.html;
location = /403.html {
root /var/www/FOLDER;
internal;
}
error_page 404 = /404.html;
location = /404.html {
root /var/www/FOLDER;
internal;
}
}
Make sure that your custom error pages are still sending the correct status codes..
Like a 403 should still be a 403 and not a 200..
I had to do it like this..
error_page 403 =403 /403.html;
Otherwise the 403 would be cached in varnish which is not cool non attacking visitors.