Bootstrap FreeKB - Nginx (Web Server) - error_page directive (401 404 500)
Nginx (Web Server) - error_page directive (401 404 500)

Updated:   |  Nginx (Web Server) articles

The error_page directives in the Nginx configuration file (e.g. nginx.conf) can be used to serve custom pages for certain HTTP response codes, such as 404 Not Found. By default, the error_page directives should have something like this.

error_page 404 /404.html;
    location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
    location = /50x.html {
}

 

With this default configuration, something like this will be displayed in the browser.

 

If the error_page directive is commented out, something like this will be displayed.

 

Instead of displaying this default output, you can create your own HTML or PHP pages. Let's say you want to display a custom HTML page for HTTP response code 404. Your own HTML file will need to be placed somewhere below the root directory. For example, if the root directory is /var/www/html, then your 404.html file could reside at /var/www/html/404.html.

root /var/www/html;
error_page 404 /404.html;

 

Create the 404.html file.

touch /var/www/html/404.html

 

Now, if a user requests a file from your web server that returns 404 Not Found, your fancy HTML page will be returned.

 


Multiple Server Blocks

Let's say your nginx configuration file has multiple server blocks, such as foo.freekb.net and bar.freekb.net. In this configuration, you would have to have two 50x.html files:

  • /var/www/foo/50x.html
  • /var/www/bar/50x.html
server {
    server_name         foo.freekb.net;
    index               index.html;
    listen              80;
    root                /var/www/foo;
    error_page          500 502 503 504 /50x.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

server {
    server_name         bar.freekb.net;
    index               index.index;
    listen              18080;
    root                /var/www/bar;
    error_page          500 502 503 504 /50x.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

 

A location block can be added to each server block to used a single 50x.html file.

  • /var/www/shared/50x.html
server {
    server_name         foo.freekb.net;
    index               index.index;
    listen              18080;
    root                /var/www/foo;

    location = /50x.html{
        root /var/www/shared;
    }
    error_page          500 502 503 504 /50x.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

 

Be aware that if the file is a PHP file, such as 50x.php, you will probably need to include the fastcgi directives in the location block.

server {
    server_name         foo.freekb.net;
    index               index.index;
    listen              18080;
    root                /var/www/foo;

    location = /50x.php{
        root /var/www/shared;
        fastcgi_index 50x.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass php.example.com:9000;
        include fastcgi_params;
    }
    error_page          500 502 503 504 /50x.php;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f9ae16 in the box below so that we can be sure you are a human.