There are many tutorials online about customizing Apache’s 404 error pages, and it’s generally not difficult. Today, I’ll mainly cover how to configure a custom 404 error page in Nginx on a Linux system. While there are many related articles online, the status code returned is often 200 (a normal status code). When search engine crawlers encounter an error page that returns a 200 status, they treat it as a normal request with a valid response and will index it. This creates a large number of duplicate pages, which is actually very detrimental to search engine optimization. Therefore, our goal today is not only to implement a custom 404 error page but also to return the proper 404 status code, clearly indicating the error state to the requester.
The correct configuration method should be as follows (omit the equals sign):
http{
…..
fastcgi_intercept_errors on;
…..
}
#—————————————-
server{
error_page 404 /data/index.html; // Note, no equals sign here
}
A smooth reload of Nginx will resolve the issue:
/usr/local/ws/nginx/sbin/nginx -s reload
Explanation: fastcgi_intercept_errors
Syntax: fastcgi_intercept_errors on | off
Default: fastcgi_intercept_errors off
Context: http, server, location
This directive specifies whether to pass 4xx and 5xx error information directly to the client, or to allow nginx to handle the error information using the error_page directive. You must explicitly specify a handler in error_page for this parameter to take effect. As Igor stated, “Without an appropriate handler method, nginx will not intercept an error; the error will not display its own default page. This allows errors to be intercepted through certain