error_page

The `error_page` directive configures custom error pages for specified HTTP status codes.

Syntaxerror_page code1 [code2 ...] uri;
Defaultnone
Contexthttp, server, location, if in location
Arguments2+

Description

The error_page directive is utilized in NGINX configurations to define custom error responses for specific HTTP status codes. This directive can be applied in different contexts such as http, server, location, or within an if block in a location block. You can specify two or more arguments: the first is the HTTP status code or range (for example, 404 or 500 502 503 504), and the second is the URI to serve as the error page, which could point to either a static file or another location block.

When an error occurs, such as a file not found (404) or a server error (500), NGINX checks the error_page directives defined for that code. If a matching error code is found, NGINX will serve the defined URI, allowing for tailored responses to the end user. The error page can either be a file on disk or a different location that processes the request further. Moreover, it can also include internal redirection to handle complex error responses more gracefully.

Using error_page effectively can enhance the user experience by providing more informative or visually appealing error responses compared to the default error messages generated by the server. Additionally, developers may link custom error pages to more detailed analytics or troubleshooting resources tied to specific error codes. This flexibility aids in improving both usability and troubleshooting for web applications as well as static websites.

Config Example

http {
    error_page 404 /custom_404.html;
    location = /custom_404.html {
        root /usr/share/nginx/html;
    }
}

Specifying a URI that does not exist can lead to recursive error handling.

Error pages with a location block must not return a 404 themselves, or it will trigger the next error handler.

Not all HTTP status codes can be used; make sure to check NGINX documentation for valid codes.

← Back to all directives