SoFunction
Updated on 2025-04-11

nginx custom 404, 50x error page implementation

Environment centos7.6, nginx 1.16.1

1. To install nginx, you need to configure the epel source (omitted)

yum install -y nginx

2. Start nginx

systemctl start nginx
systemctl enable nginx

3. Configure nginx reverse proxy http://10.2.1.16.
Enable nginx server access 10.2.1.16 port permissions (omitted)
Worker_processes 4 is configured here, and the number of root CPU cores is the same
worker_connections 1024, related to memory size
Modify the 404 html page to custom_404.html
Modify the 50x html page to custom_50x.html

[root@ansible002 ~]# cat /etc/nginx/
user nginx;
worker_processes 4;
error_log /var/log/nginx/;
pid /run/;

# Load dynamic modules. See /usr/share/doc/nginx/.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/ directory.
    # See /en/docs/ngx_core_module.html#include

    # for more information.
    include /etc/nginx//*.conf;

    server {
        listen 90;
        server_name localhost;

        include /etc/nginx//*.conf;

        location / {
        proxy_pass http://10.2.1.16;
        }
      
       #Modify the 404 html page to custom_404.html        error_page 404 /custom_404.html;
        location /custom_404.html {
        root /usr/share/nginx/html;
        internal;
        }
        
       #Modify the 50x html page to custom_50x.html        error_page 500 502 503 504 /custom_50x.html;
        location /custom_50x.html {
        root /usr/share/nginx/html;
        internal;
        }

    }
}

4. Edit 404, 50x html file

[root@ansible002 ~]# cat /usr/share/nginx/html/custom_404.html 
<h1 style='color:red'>Error 404: Not found :</h1>
<p>I have no idea where that file is, sorry.  Are you sure you typed in the correct URL?</p>
[root@ansible002 ~]# cat /usr/share/nginx/html/custom_50x.html 
<h1>Oops! Something went wrong...</h1>
<p>We seem to be having some technical difficulties. Hang tight.</p>

5. Update nginx configuration

nginx -s reload

6. Test

Visit a page that does not exist 404, e.g.http://nginx_ip/xxx
Reviseproxy_pass http://10.2.1.16forproxy_pass http://10.2.1.16:9099;, proxy a service that does not exist,nginx -s reload, Visithttp://nginx_ipTest 50x

This is the end of this article about the implementation of nginx custom 404 and 50x error pages. For more related nginx 404 and 50x error page content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!