nginx error_page注意事项

        nginx error_page对于同一个http状态码,只能执行一次,第二次就不会再执行了(初认识有错误)。

       例如:403状态

       下面这段代码,如果访问出现403,会跳到retry段,在没有,就会直接出现403,而不会跳到http://www.perofu.com/download/error403.html

              location @retry  {

            #return 409;

            root   /data1/;

            index  index.html index.htm;

            error_page 403    http://www.perofu.com/download/error403.html;

          }

          location / {

            root   /data/;

              index  index.html index.htm;

            error_page 403 = @retry ;

            error_page 404 =   http://www.perofu.com;

          }

 

今天有做了个测试,如果使用lua把状态码改成别的,再使用error_page,会不会可以呢?

 

location @retry {

                #       return 409;

                        root /data/www;

                        index index.html   index.htm index.php;

          content_by_lua '

                return   ngx.exit(410);

        ';

                        error_page 404 =   http://www.baidu.com;

                        error_page 403 =   http://danhuaer.com/;

                        error_page 410 = http://news.17173.com/game/yzdzh.shtml;

                }

 

                location / {

                        root  /data/www/www;

                        index index.html   index.htm index.php;

                        error_page 403 =   @retry;

                        error_page 404 =   http://www.perofu.com;

                  }

 

       我把/data/www/www目录下的index.html删除了,匹配到/,会出现403,再跳到retry中,使用lua返回401的状态码,看error_page是否能跳转到http://news.17173.com/game/yzdzh.shtml

 

       验证:

102858_X2gk_727880.jpg

 

       看来error_page在一次请求中,只能使用一次,无关状态码是否相同

 

       不能使用error_page,怎么才能把nginx的状态码不让用户看的,而是可以直接跳到指定的页面?

 

       当然是使用lua就行跳转了

location @retry {

                #       return 409;

                        root /data/www;

                        index index.html   index.htm index.php;

          content_by_lua '

                --return   ngx.exit(410);

                ngx.redirect("http://jd.com", 302);

        ';

                        error_page 404 =   http://www.baidu.com;

                        error_page 403 =   http://danhuaer.com/;

                        error_page 410 =   http://news.17173.com/game/yzdzh.shtml;

                }

 

                location / {

                        root  /data/www/www;

                        index index.html   index.htm index.php;

                        error_page 403 =   @retry;

                        error_page 404 =   http://dl.pconline.com.cn;

                }

 

       访问就会直接跳转到jd.com

103003_bgcM_727880.png

 

 

 

 

 

 


转载于:https://my.oschina.net/fufangchun/blog/508152

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值