1.问题描述:
就是在我的上一篇文章中,研究如何“使用nginx + lua脚本 + redis进行token鉴权”时,遇到一个大坑。nginx的location中使用rewrite_by_lua来导入lua脚本后,if条件语句失效了,到那时尝试了很多方法。最开始以为自己写错变量,符号写错,还为此删除Openresty重新安装了一遍,结果发现还是一样。在location中定义的变量,经过lua脚本修改后,if语句失效了。
2.遇坑时的代码
2.1在nginx.conf的location配置
#匹配下载请求前缀, 进行token鉴权
location ~ /download/files/.*?/data {
set $backend '';
default_type 'text/html';
rewrite_by_lua_file lua-resty-redis/lib/resty/token.lua;
#echo 可以打印变量$backend出来
echo $backend;
if ( $backend = "download"){
proxy_pass http://download;
break;
}
if ( $backend = "failure"){
rewrite ^/ /return.html;
}
}
2.2 在location中引入的token.lua
local var = ngx.var
local arg