Fri. Aug 7th, 2020

How to custom 404 page on Nginx

1 min read

Customizing the nginx 404 error page is a detail to improve the user experience. You can set the error page for each site separately, or you can set a global 404 page. To custom 404 pages, you use the below command to configure nginx config file:

sudo nano /etc/nginx/sites-enabled/default

Set a 404 page for the specified location

location /my_blog {

    error_page    404 = /article_not_found.html;

}

404 pages throughout the site

server {

listen 80;

    error_page  404  /page_not_found.html;

    …

You can handle multiple error codes together with a single error page

location /my_blog {

 error_page 500 502 503 504 = /server_error.html

}

Redirect to a completely different server, assuming you have an upstream server2 defined in the http zone:

upstream server2 {

    server 10.0.0.1:80;

}

server {

    location /my_blog {

        error_page    404 = @try_server2;

    }

    location @try_server2 {

        proxy_pass http://server2;