django - Openresty : pass a request to FastCGI if data does not found in redis cache 中带有 nginx 的 Lua

标签 django caching nginx lua redis

我有一个 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/

相关文章:

Django/Postgres 迁移失败 "django.db.utils.ProgrammingError: relation "django_site“不存在”

python - Django日期比较在服务器上不同

javascript - 如何清除 mac 上的 phantomjs 缓存?

php - Cloudflare 不会通过 CURL 请求缓存页面

php - 使用 crontab 执行 PHP 脚本时出错

python - 向 JSON 中的键值对添加标签

python - 动态更新 ModelForm 的 Meta 类

c++ - 用C++查找L1和L2缓存的大小

shell - nginx:使用环境变量

ubuntu - 我使用 nginx 创建了虚拟主机,但收到 403 Forbidden