Getting started with Nginx error page redirection (Nginx installed from yum)

1. Environment: Nginx installed from yum.
2. Scenario: redirect the error prompt page of Nginx.
3. Configuration file / etc/nginx/conf.d/default.conf
tip: why is this configuration file? Because include /etc/nginx/conf.d/*.conf is loaded in the nginx.conf file;
4. default.conf source code:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

5. Nginx access page directory: usr/share/nginx/html/
The user added 404.html file, 403.html file, 200 directory, 403 directory and 502 directory.

1. To access the localhost normal page

2. Access to localhost/200, localhost/403, localhost/502 deny access to the directory, prompt Nginx with error

3. The access to localhost/404 is not found, indicating the error of Nginx

Then we modify the default.conf
1. Remove the comment of error page 404 / 404.html; and redirect 404 error
2. Add error page 403 / 403.html; redirect error 403
3, add

location /502 {
        return 502;
    }

, access 502 directory and return 502 status code

Hot restart nginx

# service nginx reload

1. Now visit the localhost normal page
2. Access to localhost/200 and localhost/403, refuse to access the directory, and prompt to customize the page

3. Access to localhost/404 not found, prompt for custom page

4. Visit the status of localhost/502 502, and prompt to customize the page

Because of the definition

location /502 {
        return 502;
    }

So accessing localhost/502 returns 502 status,
By definition

error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

So the 502 status prompt page is 50x.html

Finally, put the modified default.conf file

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page  404              /404.html;
    error_page  403              /403.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

    location /502 {
        return 502;
    }
}


Keywords: Nginx PHP Apache yum

Added by southofsomewhere on Tue, 24 Dec 2019 23:04:12 +0200