Nginx错误页面重定向入门(yum安装的Nginx)

1、环境:yum安装的Nginx。
2、场景:重定向Nginx自带的错误提示页面。
3、配置文件:/etc/nginx/conf.d/default.conf
tip:为什么是这个配置文件呢,因为在nginx.conf文件中加载了include /etc/nginx/conf.d/*.conf;
4、default.conf源码:

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访问页面目录:usr/share/nginx/html/
自定义加添了404.html文件、403.html文件、200目录、403目录、502目录。
Nginx错误页面重定向入门(yum安装的Nginx)

1、这样访问localhost 正常页面
Nginx错误页面重定向入门(yum安装的Nginx)
2、访问localhost/200localhost/403localhost/502 拒绝访问目录,提示Nginx自带提示错误
Nginx错误页面重定向入门(yum安装的Nginx)

3、访问localhost/404 未找到,提示Nginx自带提示错误

Nginx错误页面重定向入门(yum安装的Nginx)

然后我们修改default.conf
1、去掉error_page 404 /404.html;的注释,重定向404错误
2、添加#error_page 403 /403.html;,重定向403错误
3、添加

location /502 {
        return 502;
    }

,访问502目录返回502状态码

热重启nginx

# service nginx reload

1、现在访问localhost 正常页面
2、访问localhost/200localhost/403 拒绝访问目录,提示自定义页面
Nginx错误页面重定向入门(yum安装的Nginx)
3、访问localhost/404 未找到,提示自定义页面
Nginx错误页面重定向入门(yum安装的Nginx)
4、访问localhost/502 502状态,提示自定义页面
Nginx错误页面重定向入门(yum安装的Nginx)
由于定义了

location /502 {
        return 502;
    }

所以访问 localhost/502返回502状态,
由定义了

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

所以502状态提示页面为50x.html

最后放上修改后的default.conf文件

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;
    }
}