前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx设置404错误页面跳转

Nginx设置404错误页面跳转

作者头像
拓荒者
发布2019-08-16 16:31:28
8.4K0
发布2019-08-16 16:31:28
举报
文章被收录于专栏:运维经验分享运维经验分享

Nginx设置404错误页面跳转

2018年11月03日 19:33:53 MASTERYEE 阅读数 3989

文章目录

一、Nginx在Linux上设置404错误页面

  • Linux版本:Centos 7.4
  • Nginx版本:nginx-1.14.0.tar.gz
  • nginx安装目录参考: /usr/local/nginx则是我的安装目录

说明:我Linux服务器上已经在tomcat上部署了一个项目,使用Nginx进行的代理, 访问项目不存在的页面时,出现的是Nginx默认的404页面,现在我配置我自己写的404页面进行提示

注意:网上大多数博客写的都只有一种情况,要么就是使用 proxy_intercept_errors on;, 要么就是使用fastcgi_intercept_errors on; 没有说明这两种的区别, 还有也没有说明404.html文件应该放在服务器的什么位置,我在此处优先进行说明, 如果你本地有部署项目,优先使用proxy_intercept_errors on;这个配置进行尝试, 如果没有部署项目,则使用fastcgi_intercept_errors on; 这个进行尝试,也可以两个全加上, 其次404.html文件放在nginx安装目录的html文件夹下

1.1 第一种配置情况(跳转网络地址)

error_page配置的是http这种的网络地址

在http下配置 proxy_intercept_errors on;

代码语言:javascript
复制
http {
    include       mime.types;
    default_type  application/octet-stream;

	proxy_intercept_errors on;
	... 以下省略
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在server下配置 error_page 以下三种情况都可以起作用, 可以配置在server第一层的任何位置, 不受影响 也可以配置在location里面,我下面代码注释的地方都是可以配置的

代码语言:javascript
复制
  server {
        listen       80;
        server_name  www.xxxxxxx.com;
        #error_page  404  http://www.baidu.com;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://search-masteryee;
			proxy_set_header   REMOTE-HOST $remote_addr;
			proxy_set_header   Host $host; 
			proxy_set_header   X-Real-IP $remote_addr; 
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
			client_max_body_size    20m; 
			#error_page  404  http://www.baidu.com;
        }
		
		location /upload {
            root   /usr/;
            index  index.html index.htm;
        }
		
        error_page  404  http://www.baidu.com;
       
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

1.2 第二种配置情况(跳转本地地址)

error_page配置的是本地服务器的页面地址,

  • 说明:我的404.html页面文件放在nginx安装目录下的html文件夹内
  • 如果编写的404.html页面中有图片等外部文件,使用相对地址是不行的

在http下配置 proxy_intercept_errors on;

代码语言:javascript
复制
http {
    include       mime.types;
    default_type  application/octet-stream;

	proxy_intercept_errors on;
	... 以下省略
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在server中配置error_page 说明:我的nginx安装在/usr/local/下

代码语言:javascript
复制
    server {
        listen       80;
        server_name  www.xxxxxxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://search-masteryee;
			proxy_set_header   REMOTE-HOST $remote_addr;
			proxy_set_header   Host $host; 
			proxy_set_header   X-Real-IP $remote_addr; 
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
			client_max_body_size    20m; 
        }
		
		location /upload {
            root   /usr/;
            index  index.html index.htm;
        }

        
		error_page   404  /404.html;
        location = /404.html {
            #使用绝对地址, 跳转服务器/usr/local/nginx/html/404.html
            root   /usr/local/nginx/html;
        }
        
        # 这种方式和上面的方式均可起作用,只需要选择一种即可,本文中没有进行进一步注释
        error_page   404  /404.html;
        location = /404.html {
            # 使用相对地址, 跳转nginx安装目录下的html/404.html
            root   html;
            # 下面这种多了一个/ 反而不起作用
            #root   /html;
        }
        
        # 以下这几种网上比较多的方式,均试过,无法跳转正确页面或不起跳转作用
        #error_page   404  404.html;
        #error_page   404  /404.html;
        #error_page   404  html/404.html;
        #error_page   404  /html/404.html;
        #error_page   404  /usr/local/nginx/html/404.html;
        #error_page   404  usr/local/nginx/html/404.html;
        
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

可以配置多种返回码的多个错误页面,也可以同时配置多个错误码跳转一个页面,可以同时存在 如下所示

代码语言:javascript
复制
server {
        listen       80;
        server_name  www.xxxxxxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://search-masteryee;
			proxy_set_header   REMOTE-HOST $remote_addr;
			proxy_set_header   Host $host; 
			proxy_set_header   X-Real-IP $remote_addr; 
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
			client_max_body_size    20m; 
        }
		
		location /upload {
            root   /usr/;
            index  index.html index.htm;
        }

        #error_page  404	/404.html;
		
		# 错误页面的种类也可以是多个
		
		# 这里的错误码可以是多个
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        # 这里是错误吗也可以是一个
		error_page   404  /404.html;
        location = /404.html {
            root   html;
        }

       
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

1.3 第三种情况(tomcat未启动时)

当我把我的tomcat服务器关掉时,我服务器就没有运行项目了,这时在访问页面,则上述配置没有产生效果,此时则需要添加一个配置 fastcgi_intercept_errors on;

在http下配置 fastcgi_intercept_errors on;

代码语言:javascript
复制
http {
    include       mime.types;
    default_type  application/octet-stream;

	fastcgi_intercept_errors on;
	proxy_intercept_errors on;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

server中配置如下,将500 502 503 504等状态码一致性跳转404页面

代码语言:javascript
复制
server {
        listen       80;
        server_name  www.xxxxxxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://search-masteryee;
			proxy_set_header   REMOTE-HOST $remote_addr;
			proxy_set_header   Host $host; 
			proxy_set_header   X-Real-IP $remote_addr; 
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
			client_max_body_size    20m; 
        }
		
		location /upload {
            root   /usr/;
            index  index.html index.htm;
        }

        #error_page  404	/404.html;
		
		error_page   500 502 503 504  /404.html;
		error_page   404  /404.html;
        location = /404.html {
            root   html;
        }

       
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1.4 第四种情况(proxy_intercept_errors的配置地址可多样)

proxy_intercept_errors on;这个配置不一定需要放在http下面,也可以是server下,也可以是server的location下

代码语言:javascript
复制
server {
        listen       80;
        server_name  www.masteryee.com;
		proxy_intercept_errors on;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://search-masteryee;
			proxy_set_header   REMOTE-HOST $remote_addr;
			proxy_set_header   Host $host; 
			proxy_set_header   X-Real-IP $remote_addr; 
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
			client_max_body_size    20m; 
			#可以是这里
			#proxy_intercept_errors on;
        }
		location /upload {
            root   /usr/;
            index  index.html index.htm;
        }
		
		error_page   500 502 503 504  /404.html;
		error_page   404  /404.html;
        location = /404.html {
            root   html;
        }
       
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

1.5 proxy_intercept_errors和fastcgi_intercept_errors的理解

配置proxy_intercept_errors on; 时配置的错误页面表示的是当服务器返回的状态码为我们配置的状态码时,我们才进行的页面跳转。如:服务器中没有xxxx.do接口时,我们访问了这个接口,配置了 proxy_intercept_errors on;则也会进行页面跳转

如果服务器中没有开启服务,则配置proxy_intercept_errors on; 无用,则需要再添加fastcgi_intercept_errors on; 配置, 这样的话,出现页面错误时也会进行跳转

参考1

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Nginx设置404错误页面跳转
    • 文章目录
    • 一、Nginx在Linux上设置404错误页面
      • 1.1 第一种配置情况(跳转网络地址)
        • 1.2 第二种配置情况(跳转本地地址)
          • 1.3 第三种情况(tomcat未启动时)
            • 1.4 第四种情况(proxy_intercept_errors的配置地址可多样)
              • 1.5 proxy_intercept_errors和fastcgi_intercept_errors的理解
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档