作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个 Django 网站,它使用 fcgi 在 Nginx 上运行。
对于 url /gifts/
我想通过使用 openresty 在 nginx.conf 文件中将一些逻辑实现到 lua 中。
location /gifts { try_files $uri @redis_cache; } location @redis_cache { default_type text/html; content_by_lua ' -- fetching key and values from url local args = ngx.req.get_uri_args() --creating redis connection local redis = require "resty.redis"; local red = redis:new() red:set_timeout(1000) -- 1 sec local ok, err = red:connect("127.0.0.1", 6379) if not ok then ngx.log(ngx.ERR, err, "Redis failed to connect") return ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE) end if not args["key"] then return ngx.exit(ngx.HTTP_NOT_FOUND) end if args["value"] then local ok, err = red:set(args["key"], args["value"]) end if not ok then ngx.say("Please pass key value pair to store in cache", err) end -- getting data from redis cache local res, err = red:get(args["key"]) if not res then return ngx.say("value is not in redis cache", err, "|") end ngx.say("Value found in Redis is: ", res) '; }
Everythig 按要求工作正常,但有一个问题,如果缓存在 Redis 中不可用,我想将请求重定向到 fcgi。
请帮助我如何进行此操作。
最佳答案
如果您只想提供来自 fcgi 的内容,请使用 internal redirection .如果你还想从 Lua 中填充缓存,你应该看看 subrequests相反。
关于django - Openresty : pass a request to FastCGI if data does not found in redis cache 中带有 nginx 的 Lua,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19996498/
我是一名优秀的程序员,十分优秀!