码迷,mamicode.com
首页 > 其他好文 > 详细

006-nginx.conf详解-error_page 使用

时间:2019-12-13 14:07:34      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:tar   文件   NPU   指令   本地   ror   com   控制   作用   

一、概述

  nginx指令error_page的作用是当发生错误的时候能够显示一个预定义的uri

 

1.1、使用步骤

  更改nginx.conf在http定义区域加入: proxy_intercept_errors或者fastcgi_intercept_errors

fastcgi_intercept_errors on;#默认off

  如果没这句的不管是error_page 还是nginx自带的404跳转都不能正常显示(访问不存在的页面时可能会显示“No input file specified.”)

  • 默认: fastcgi_intercept_errors off
  • 添加位置: http, server, location, location 中的if字段 
  • 默认情况下,nginx不支持自定义404错误页面,只有这个指令被设置为on,nginx才支持将404错误重定向

1.2、配置全局错误【可不配置】

  可以在http下添加

error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = http://www.github.com/404.html;

1.3、针对http中server具体配置error_page

  更改nginx.conf,在server 区域加入: error_page 404  /404.html  或者 error_page 404 =http://www.xxx.com/404.html

1.3.1、方式一、本地文件-直接读取本地物理文件

    error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 /404.html;

    location =/404.html {
        root /export/servers/nginx/html;
    }

  实际上产生了一个内部跳转(internal redirect),当访问出现上述错误码的时候就能返回404.html中的内容,这里需要注意是否可以找到404.html页面,所以加了个location保证找到你自定义的404页面。

1.3.2、方式二、指向具体url

error_page 404 http://www.xxx.com/404.html

1.3.3、方式三、设置named location,然后在里边做对应的处理。

error_page 500 502 503 504 @jump_to_error;
location @jump_to_error {    
    proxy_pass http://backend;
}

同时error_page在一次请求中只能响应一次,对应的nginx有另外一个配置可以控制这个选项:recursive_error_pages

默认为false,作用是控制error_page能否在一次请求中触发多次。

1.4、重定义响应码【使用等号】

1、自己定义返回状态码【等号后面追加响应码 如 =200】

    error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 =200 /404.html;

    location =/404.html {
        root /export/servers/nginx/html;
    }

  这样用户访问产生上述响应码的时候给用户的返回状态是200,内容是404.html。

2、使用要访问页状态码【等号后面不写响应码 如 =】

    error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 504 506 = /404.html;

    location =/404.html {
        root /export/servers/nginx/html;
    }

 

006-nginx.conf详解-error_page 使用

标签:tar   文件   NPU   指令   本地   ror   com   控制   作用   

原文地址:https://www.cnblogs.com/bjlhx/p/12034531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!