Nginx核心配置-自定义错误页面

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  生产环境中错误页面一般都是UI或开发工程师提供的,他们已经在软件中定义好了,我们这里就简单写个html页面就好啦。

一.编辑nginx的配置文件

1>.查看nginx的主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-8;
keepalive_timeout 65 65; #导入其他路径的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.查看nginx的子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/auth.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; error_page 500 502 503 504 404 /error.html; location / {
root /yinzhengjie/data/web/nginx/html;
index index.html;
} location /login {
root /yinzhengjie/data/web/nginx;
index index.html;
deny 172.30.1.108;
allow 172.30.1.0/24;
allow 2001:0db8::/32;
deny all;
} location /error.html {
root /yinzhengjie/data/web/nginx/html/404;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

3>.准备数据文件(只需要准备第二步的错误页面相关文件即可)

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/html/ -R          #目前已经存在的文件,之所以让大家看看我的目录结构,是因为我第三步会专门访问web服务器中不存在的资源。
/yinzhengjie/data/web/nginx/html/:
total 608
-rw-r--r-- 1 root root 122026 Dec 16 19:35 01.jpg
drwxr-xr-x 2 root root 24 Dec 17 10:45 404
-rw-r--r-- 1 root root 248743 Dec 17 10:44 404.jpg
-rw-r--r-- 1 root root 122026 Dec 16 20:10 Fg.jpg
-rw-r--r-- 1 root root 122026 Dec 16 20:03 Fg.JPG
drwxr-xr-x 2 root root 38 Dec 16 19:35 image
-rw-r--r-- 1 root root 88 Dec 15 23:13 index.html
drwxr-xr-x 2 root root 34 Dec 16 20:40 static /yinzhengjie/data/web/nginx/html/404:
total 4
-rw-r--r-- 1 root root 1717 Dec 17 10:44 error.html /yinzhengjie/data/web/nginx/html/image:
total 248
-rw-r--r-- 1 root root 248743 Dec 16 19:35 01.jpg
-rw-r--r-- 1 root root 49 Dec 15 23:07 index.html /yinzhengjie/data/web/nginx/html/static:
total 364
-rw-r--r-- 1 root root 248743 Dec 16 20:40 01.jpg
-rw-r--r-- 1 root root 122026 Dec 16 20:40 02.jpg
[root@node101.yinzhengjie.org.cn ~]#

4>.重新nginx服务

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 3330 2840 0 10:35 ? 00:00:00 nginx: worker process
nginx 3331 2840 0 10:35 ? 00:00:00 nginx: worker process
nginx 3332 2840 0 10:35 ? 00:00:00 nginx: worker process
nginx 3333 2840 0 10:35 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 3566 2840 7 10:56 ? 00:00:00 nginx: worker process
nginx 3567 2840 6 10:56 ? 00:00:00 nginx: worker process
nginx 3568 2840 8 10:56 ? 00:00:00 nginx: worker process
nginx 3569 2840 8 10:56 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

二.自定义错误页面

1>.编辑error.html文件的内容

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/html/404/
total 4
-rw-r--r-- 1 root root 1717 Dec 17 10:44 error.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/nginx/html/404/error.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>404-对不起!您访问的页面不存在</title> <style type="text/css"> .head404{ width:580px; height:234px; margin:50px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/head404.png) no-repeat; } .txtbg404{ width:499px; height:169px; margin:10px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/txtbg404.png) no-repeat;} .txtbg404 .txtbox{ width:390px; position:relative; top:30px; left:60px;color:#eee; font-size:13px;} .txtbg404 .txtbox p {margin:5px 0; line-height:18px;} .txtbg404 .txtbox .paddingbox { padding-top:15px;} .txtbg404 .txtbox p a { color:#eee; text-decoration:none;} .txtbg404 .txtbox p a:hover { color:#FC9D1D; text-decoration:underline;} body {
background-image: url("/404.jpg");
}
</style> </head> <body bgcolor="#494949"> <div class="head404"></div> <div class="txtbg404"> <div class="txtbox"> <p>对不起,您请求的页面不存在、或已被删除、或暂时不可用,请及时联系运维人员处理~</p> <p class="paddingbox">请点击以下链接继续浏览网页</p> <p>》<a href="https://www.cnblogs.com/yinzhengjie/">返回上一页面</a></p> <p>》<a href="https://www.cnblogs.com/yinzhengjie/">返回网站首页</a></p> </div> </div> </body> </html>
</html>
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.上面的error.html的背景图像我换成了一张图片,在上面写的是"/404.jpg",因此我们得取匹配web服务器的"/"路径下是否有一个404文件哟~

三.客户端测试验证

1>.客户端访问正常存在的资源,如下图所示,可以正常显示

2>.客户端访问不存在的页面,如下图所示,显示的是咱们规定的内容

Nginx 核心配置-自定义错误页面的更多相关文章

  1. Nginx 核心配置-自定义日志路径及清空日志注意事项

    Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...

  2. 【Azure 应用服务】App Service中,为Java应用配置自定义错误页面,禁用DELETE, PUT方法

    问题定义 使用Azure应用服务(App Service),部署Java应用,使用Tomcat容器,如何自定义错误页面呢?同时禁用DELETE, PUT方法 解决办法 如何自定义错误页面呢?需要在 J ...

  3. NGINX下配置404错误页面的方法分享

    NGINX下配置自定义的404页面是可行的,而且很简单,只需如下几步,需要的朋友可以参考下   1. 创建自己的404.html页面 2.更改nginx.conf在http定义区域加入: fastcg ...

  4. nginx中配置404错误页面的教程

    什么是404页面如果网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是NGI ...

  5. ASP.NET全局错误处理和异常日志记录以及IIS配置自定义错误页面

    应用场景和使用目的 很多时候,我们在访问页面的时候,由于程序异常.系统崩溃会导致出现黄页.在通常的情况下,黄页对于我们来说,帮助是极大的,因为它可以帮助我们知道问题根源,甚至是哪一行代码出现了错误.但 ...

  6. nginx下配置404错误页面

    1.创建自己的404.html页面,并放于网站根目录. 2.更改nginx.conf在http定义区域加入: fastcgi_intercept_errors on; 3.更改nginx.conf(或 ...

  7. cdnbest独立主控配置自定义错误页面

    注:自定义错误的域名最好配置ssl,不配碰到有使用ssl的域名,错误码显示就会有问题 (1)登陆管理后台点区域列表-->配置-->错误url 解析一个域名到你的cdn主控,然后输入这个ur ...

  8. nginx如何配置网页错误页面

    首先要在http模块中加入 fastcgi_intercept_errors on; 其次要在server模块中加入 error_page 403 404 /40x.html; location = ...

  9. NGINX 配置404错误页面转向

    什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...

随机推荐

  1. Spring事务异常rollback-only 笔记

    造成以上异常的原因情形: 在spring里面我们配置了事务的传播机制是REQUIRED,所以这两个事务最终会合并成一个事务.当a方法调用b方法时,程序中a方法中由于某某原因导致抛出异常(或者明确将该事 ...

  2. SWA2G422&485JK2G基础篇: STM32+W5500实现MQTT通信控制,485/422透传通信

    说明 这节实现的功能: STM32+W5500实现MQTT通信控制 细节功能: 1.DHCP动态获取IP 2.DNS域名解析 3.网口<--MQTT-->485/422透传通信 测试准备工 ...

  3. 11/2 下午 <String>

    344. Reverse String 解法一(暴力法): 直接从两头往中间走,同时交换两边的字符即可 首位对调位置. class Solution { public void reverseStri ...

  4. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  5. 测开面试 | Python语言常见问题

    1.面向对象的概念? 面向对象编程,简称OOP,是一种程序设计思想 主要包括:类.继承.多态(子类方法覆盖父类方法).实例.属性.方法 2.什么是进程.线程.协程? 进程:独立数据空间,进程间不共享数 ...

  6. 修改hadoop/hbase/spark的pid文件位置

    1.说明 当不修改PID文件位置时,系统默认会把PID文件生成到/tmp目录下,但是/tmp目录在一段时间后会被删除,所以以后当我们停止HADOOP/HBASE/SPARK时,会发现无法停止相应的进程 ...

  7. Visual Studio 调试系列12 远程调试部署在远程计算机IIS上的ASP.NET应用程序

    系列目录     [已更新最新开发文章,点击查看详细] 要调试已部署到IIS的ASP.NET应用程序,请在部署应用程序的计算机上安装并运行远程工具,然后从Visual Studio附加到正在运行的应用 ...

  8. vue 学习注意事项

    一:插值方式: 1:数据绑定,最常见的形式就是使用 “Mustache” 语法(双大括号)的文本插值 <span>Message: {{ msg }}</span>  通过使用 ...

  9. Feign的介绍和使用

    一.Feign的简介 Feign是一个声明式 WebService 客户端,使用Feign能够让编写Web Service 客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解,同时也支持J ...

  10. Python基础 — 面向对象编程基础

    目录 1. 面向对象编程基础 2. 定义类和创建对象 3. init() 方法 4. 魔法方法 5. 访问可见性问题 5. 练习 1. 面向对象编程基础 把一组数据结构和处理它们的方法组成对象(obj ...