补充下,需求是判断域名的合法性,不正确的域名跳转到错误页面,因为每次请求都需要判断域名,所以代码会多次调用,不知道公共代码怎么写,因为公用代码还需要返回一个变量值,不知道变量怎么去传递。。。
在 2013年10月3日星期四UTC+8下午7时13分14秒,bjd...@gmail.com写道:
listen 80;
location @cats-by-id {
# 初始化值
set $root_dir "/tmp";
if ($host ~* ^([a-z]\w\w+)\.123\.cn$) {
set $root_dir "";
access_by_lua '
-- 内部通过memcache获取数据,假定已经获取到
res = "aaa"
//赋值 没法传递
ngx.var.root_dir = res
// 用返回值的方法也没法传递
return res;
';
}
#外层判断
if ($root_dir = "/tmp") {
break;
}
}
# 因为每个location 都得判断,还有php已经静态文件接口都需要
#不知道怎么去复用代码
location / {
set $root_dir "/tmp";
access_by_lua '
ngx.var.root_dir = ngx.exec("@cats-by-id")
';
echo $root_dir;
#root $root_dir;
#index index.html index.htm;
#echo_before_body $root_dir;
}
location ~ \.php$ {
root $root_dir;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}