Nginx 网站定义自己的错误页面

15 篇文章 0 订阅
12 篇文章 1 订阅

场景:

为了给用户较好的交互和感官,我们通常需要对错误页面进行友好提示。

环境介绍:

LNMP(linux(centos7.4)Nginx Mysql5.6 php7.0)


实现:

这里,我直接对nginx的子配置文件进行了相应配置,给出代码

server {
    listen       80;
    server_name  www.xiaobudiu.top;

    charset utf-8;
    access_log  /etc/nginx/logs/access/www.xiaobudiu.top.access.log main;
    error_log   /etc/nginx/logs/error/www.xiaobudiu.top.error.log debug;

    root    /data/www;

    index  index.html index.htm index.php;

    location /favicon.ico {
            log_not_found off;
            access_log off;
    }

   location ~ \.php$ {
        fastcgi_pass   unix:/dev/shm/php-cgi.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
   }

   error_page  404 403 500 502 503 504  /404.html;

   location = /404.html {

        root   /data/errorPage;

   }

   location ~ /\.ht {
       deny  all;
   }
}

从上面可以看出,如果访问我定义的server(www.xiaobudiu.top)出现404,403,500,502,503,504 错误时,直接nginx重写到 location = /404.html ,在这个location中,我定义root,也就是我们自己定义的错误页面所在的位置,这里是/data/errorPage,然后我们在这个路径下vim 404.html就可以了 。

文件结构是这样:



效果示例:

假设我在我的网站找一个不存在的页面,就会直接返回我刚才自己定义的404.html,如图。



注:当然,还有对nginx反向代理错误页面的定义,以及nginx解析php出错的错误页面的定义,如果有这方面需求,可以参考这篇文章。https://www.cnblogs.com/paul8339/p/7389422.html


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值