更改Nginx的原生错误提示

注意:本文最后更新于 2530 天前,有关的内容可能已经发生变化,请参考使用。

一般网站前端使用Nginx时,不可避免地会遇到一些类似502、504、404、403等等的错误,这些生硬的错误提示看上去给人的感觉有些不太友好,虽然对于一些常见的错误页面比如404可以在Nginx的配置文件中指定错误页面,但是这只能解决一部分问题而且相对比较繁琐,实际上我们可以通过修改Nginx的源码让Nginx提供原生的友好的HTML提示。

相关内容如下:

    
    static char ngx_http_error_501_page[] =
    “<html>” CRLF
    “<head><title>501 Method Not Implemented</title></head>” CRLF
    “<body bgcolor=”white”>” CRLF
    “<center><h1>501 Method Not Implemented</h1></center>” CRLF
    ;
    
    static char ngx_http_error_502_page[] =
    “<html>” CRLF
    “<head><title>502 Bad Gateway</title></head>” CRLF
    “<body bgcolor=”white”>” CRLF
    “<center><h1>502 Bad Gateway</h1></center>” CRLF
    ;
    
    static char ngx_http_error_503_page[] =
    “<html>” CRLF
    “<head><title>503 Service Temporarily Unavailable</title></head>” CRLF
    “<body bgcolor=”white”>” CRLF
    “<center><h1>503 Service Temporarily Unavailable</h1></center>” CRLF
    ;
    
    static char ngx_http_error_504_page[] =
    “<html>” CRLF
    “<head><title>504 Gateway Time-out</title></head>” CRLF
    “<body bgcolor=”white”>” CRLF
    “<center><h1>504 Gateway Time-out</h1></center>” CRLF
    ;
    
    static char ngx_http_error_507_page[] =
    “<html>” CRLF
    “<head><title>507 Insufficient Storage</title></head>” CRLF
    “<body bgcolor=”white”>” CRLF
    “<center><h1>507 Insufficient Storage</h1></center>” CRLF
    ;
    

这就是写死在代码里面的HTML,将其改为你想要的内容。

PS:在FreeBSD下使用PORTS安装Nginx也一样可以修改Ngnix的源码达到相应的效果,只要是make完成之前将PORTS目录下的源码修改即可。


「倘若有所帮助,不妨酌情赞赏!」

Holmesian

感谢您的支持!

使用微信扫描二维码完成支付


相关文章

发表新评论