• 关于nginx自定义错误页及停机维护页的配置笔记


    终于有时间对nginx的错误页和停机维护页进行简单的优化和配置,看起来比原始的提示信息友好多了。

    首先,在nginx的安装目录创建相关的配置文件:errpage.conf

    error_page	403		/403.html;
    error_page	404		/404.html;
    error_page	500 502 503 504	/50x.html;
    location /lost {
    	root	.;
    }
    location = /403.html {
    	root	lost;
    }
    location = /404.html {
    	root	lost;
    }
    location = /50x.html {
    	root	lost;
    }
    location = /off.html {
    	root	lost;
    }

    这个文件里配置了 3 个错误状态的内部跳转,其中:

    • 403:表示无法列出目录明细;
    • 404:表示目标文件不存在;
    • 50x:表示服务端的错误,简单起见统一使用一个页面即可。

    另外,还配置了 5 个路径匹配模板,其中 3 个用于响应上面的错误页,另外 2 个:

    • /lost:用于访问各错误页中的静态资源,包括图片、css、js等;
    • /off.html:用于响应停机维护页

    配置文件准备好之后,下面就需要对各页面进行实现了,完成之后的目录结构:

    看一下off页的运行效果:

    需要注意的是,在设计页面时,为了方便预览,页面内的静态资源可直接使用相对路径下的文件名,但设计完成后,部署到nginx时,都需要使用 /lost/xxx.png 模式的路径,因为所有的静态资源都被配置到了 /lost 目录下。

    另外,lost目录的部署位置:

    • windows:默认是 nginx 的根目录,就是nginx.exe文件所在的目录;
    • linux:默认是 /usr/share/nginx 目录下。

    最后,在nginx的主配置文件中引入上面的配置文件:

    #主服务
    server {
        listen		80;
        server_name	localhost;
        location / {
            #try_files ''	/off.html;
            #proxy_pass	http://www.baidu.com;
            root		html;
            index		index.html index.htm default.html default.htm;
            expires		3d;
        }
    	include		errpage.conf;
    }

    已被注释掉的 try_files,就是用来展示停机维护页的,把这行的注释删掉,则所有请求都将被转到上面的停机维护页。

    如果采用的是 nginx + tomcat 的模式,还需要在 nginx 的配置文件中添加这行代码,作用是使用 nginx 来拦截容器的 404 等错误:

    proxy_intercept_errors	on;

    如果不加这行,还是会显示 tomcat 默认的404页面。

  • 相关阅读:
    SEO分享:我为什么会有这么多的优质外链资源?
    执行shell脚本提示“syntax error near unexpected token for((i=0;i<$length;i++))”
    Codeforces Round #254 (Div. 2)D(预计)
    自己写配置文件
    软件測试基本方法(二)之白盒測试
    hdu 4638 Group
    影视集结号--首页
    2015阿里巴巴秋招在线笔试题
    php 抓取天气情况 www.weather.com.cn
    C语言中的enum(枚举)使用方法
  • 原文地址:https://www.cnblogs.com/netWild/p/15992761.html
Copyright © 2020-2023  润新知