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?
验证:
看来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。