Nginx重定义404错误页面

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

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

以Nginx为例进行展示;

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

[root@dn2 nginx]# cat /data/nginx/conf/vhosts/default.conf
# Second Configure File
server {
  listen 80 default_server;
  root "/data/www";
}
[root@dn2 nginx]# cat /data/www/index.html
<h1>Hello World!</h1>
[root@dn2 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错误要指向的位置:

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

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

[root@dn2 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>

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

[root@dn2 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
[root@dn2 nginx]# /data/nginx/sbin/nginx -s reload
[root@dn2 nginx]# 

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

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

[root@dn2 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功能:

[root@dn2 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,这里就不用再做修改;重新加载生效即可:

[root@dn2 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
[root@dn2 nginx]# /data/nginx/sbin/nginx -s reload
[root@dn2 nginx]# 

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

《Nginx重定义404错误页面》有17条评论

  1. I was looking at some of your posts on this website and I think this website is real instructive! Continue posting . Louella Herrick Redmer

    回复
  2. But wanna remark on few general things, The website pattern is perfect, the written content is really good : D. Angelina Bradly Elman

    回复
  3. These are genuinely fantastic ideas in on the topic of blogging. You have touched some pleasant things here. Any way keep up wrinting. Polly Thebault Meggy

    回复
  4. I for all time emailed this web site post page to all my associates, for the reason that if like to read it next my links will too. Juliet Cornall Sharai

    回复
  5. 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!

    回复
  6. You made some nice points there. I looked on the internet for the subject matter and found most individuals will agree with your site.

    回复
  7. Keep up the great piece of work, I read few articles on this website and I think that your blog is really interesting and has lots of superb info . Roch Arnoldo Gretchen

    回复
  8. Helpful information. Fortunate me I found your web site by accident,
    and I am shocked why this twist of fate did not took
    place earlier! I bookmarked it.

    回复
  9. Hi there! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up losing a few months of hard work
    due to no backup. Do you have any solutions to stop hackers?

    回复
    • I’m sorry I’m afraid don’t have suggested for you.
      Perhaps following operation can help you:
      1. Firewall (including network and OS)
      2. CDN (can hide your server)
      3. Antivirus software
      By the way, remote backup is often useful.

      回复
  10. Thanks for a marvelous posting! I serriously ejoyed reding
    it, you maay bbe a great author. I will always bookkmark your blog and wkll come back someday.
    I want to encfourage you to definitely continue your great job, have a nice afternoon!

    回复

回复 altyazili izle 取消回复

error: Content is protected !!