Nginx重定义404错误页面

默认的网站服务程序(如Apache,Nginx,IIS等)处理时遇到错误时会报出相关错误页面,如404,50x等页面。

而这些页面默认会显示出相关服务器/程序相关信息,而有时依据安全需要,我们要隐藏/重定向这些信息;404错误的重定向方法有的会在网站程序中自行判断后显示预定义的错误页面,有的没有做此类操作的就需要手动在Web服务端中进行配置错误页面重定向。

以Nginx为例进行展示;

默认Nginx处理静态文件目录情况时:

[[email protected] nginx]# cat /data/nginx/conf/vhosts/default.conf
# Second Configure File
server {
  listen 80 default_server;
  root "/data/www";
}
[[email protected] nginx]# cat /data/www/index.html
<h1>Hello World!</h1>
[[email protected] nginx]#  

出现404错误会显示:

要重新定义404页面,需先在http段内开启fastcgi_intercept_errors功能:

vim /data/nginx/conf/nginx.conf
# Nginx Main Configure File.
...
http {
...
    fastcgi_intercept_errors on;
    include vhosts/*.conf;
}

然后在相关server段内使用error_page定义404错误要指向的位置:

[[email protected] nginx]# cat /data/nginx/conf/vhosts/default.conf 
# Second Configure File
server {
        listen 80 default_server;
        root "/data/www";
        error_page 404 /404.html;
}
[[email protected] nginx]# 

创建自定义的404页面文件:

[[email protected] nginx]# vim /data/www/404.html
<!DOCTYPE html PUBLIC>
<html>
<head>
<meta charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>404 - Not Found!</title>
</head>
<body bgcolor="#494949">
        <h1 style="color:white; text-align:center">对不起,您请求的页面不存在、或已被删除、或暂时不可用</h1>
</body>
</html>

测试配置,重新加载生效:

[[email protected] nginx]# /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[[email protected] nginx]# /data/nginx/sbin/nginx -s reload
[[email protected] nginx]# 

再访问不存在的地址,即会出现自定义的404页面内容:

上面是本地页面服务的情况下,如果经过upstream后proxy到其它服务上时,是不生效的;如下:

[[email protected] nginx]# vim /data/nginx/conf/vhosts/default.conf
# Second Configure File
upstream test {
        server 172.16.220.106:8284;
}
server {
        listen 80 default_server;
        
        error_page 404 /404.html;
    location = /404.html {
        root /data/www;
    }
    
    location / {
        proxy_set_header Host $host;
        proxy_pass http://test;
    }
}

访问不存在的地址会出现后端服务的404报错:

如果要重定向proxy后端服务页面中的404错误,需要在http段中开启proxy_intercept_errors功能:

[[email protected] nginx]# vim /data/nginx/conf/nginx.conf
# Nginx Main Configure File.
...
http {
...
    fastcgi_intercept_errors on;
    proxy_intercept_errors on;

    include vhosts/*.conf;
}

上面子配置文件中已经设置了error_page,这里就不用再做修改;重新加载生效即可:

[[email protected] nginx]# /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[[email protected] nginx]# /data/nginx/sbin/nginx -s reload
[[email protected] nginx]# 

刷新刚才的地址,即显示自定义的404界面:

《Nginx重定义404错误页面》有7条留言

  1. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

    回复

留下评论

error: Content is protected !!