Hello!
2012/10/23 luobo525 :
> 因为业务的需求,我用ngx+lua作了一个图片的映射跳转
>
> 需求就是:一张图片的地址是 http://xxx.com/A.jpg , 想用
> http://xxx.com/B.jpg 也能访问到A.jpg图片
>
> 所以在Redis里做了一个映射,B->A
>
> 在访问B.jpg的时候,lua访问redis,获得真实的文件名,然后
> ngx.location.capture过去
>
> 但是发现在IE浏览器下偶尔会出现红X,火狐和chrome都是正常的
>
> 能帮我看看是什么问题吗?
>
你能给出 IE 请求出错时,nginx
错误日志中的相关错误消息么?(如果有的话)一个明显的错误是你并没有把子请求的响应头传递给父请求。因为子请求的响应头并不会自动为父请求所继承。写成代码类似这个样子:
local res = ngx.location.capture(...)
...
for k, v in pairs(res.headers) do
ngx.header[k] = v
end
细节可以参见
http://wiki.nginx.org/HttpLuaModule#ngx.header.HEADER
另外,如果你只是进行动态 URI 改写的话,可以直接使用 ngx.exec() 接口来发起“内部跳转”,而不是
ngx.location.capture() 来发起 nginx 子请求:
http://wiki.nginx.org/HttpLuaModule#ngx.exec
因为内部跳转更为高效,不用在 Lua 中捕捉请求的响应,同时不必考虑把子请求的响应头自己转发给主请求的响应头这样的问题。
> 代码如下:
>
> content_by_lua '
> local uri = ngx.var.document_uri;
> local redis = require "resty.redis"
> local red = redis:new()
> red:set_keepalive(0, 100)
值得一提的是,你这里的这个 set_keepalive 调用是无效操作。你应当在使用完 red 对象之后调用之。细节可以参见文档:
https://github.com/agentzh/lua-resty-redis#set_keepalive
> red:set_timeout(1000)
> local ok = red:connect("10.15.184.51", 6379)
> if ok then
> local newuri = red:get("fmap:" .. uri)
> if not newuri or newuri == ngx.null then
> ngx.exit(404);
> else
> res = ngx.location.capture(newuri)
> if res.status == 200 then
> ngx.print(res.body)
> elseif res.status == 404 then
> ngx.exit(404);
> end
> end
> end
> ';
>
> 非常感谢~
>
同时抄送给 openresty 中文邮件列表:https://groups.google.com/group/openresty
(国内访问可能需要翻墙)建议加入此列表并在那里和我们交流 openresty 或者 nginx 的相关问题。谢谢合作 :)
Best regards,
-agentzh