2013-11-15 11 views
1

我有一個使用fcgi在Nginx上運行的Django網站。
對於url /gifts/我想通過使用openresty實現一些邏輯到lua裏面的nginx.conf文件。在openresty中使用nginx的Lua:如果在Redis緩存中找不到數據,則將請求傳遞給FastCGI

 

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是工作的罰款按規定,但有一個問題,我想重定向請求FCGI如果緩存不可爲Redis的。
請幫我解決這個問題。

回答

0

如果您只想提供fcgi的內容,請使用internal redirection。如果你還想從Lua填充緩存,你應該看看subrequests

+0

謝謝。你能爲子請求提供任何示例代碼嗎?感謝你 –

+0

就像這樣:'local value = ngx.location.capture(「/ my_fcgi_url」)。紅色:set(args [「key」],value);返回ngx.say(「來自FCGI的值:」,value)'當然還要添加錯誤檢查等。請閱讀'ngx.location.capture'文檔。 – catwell

相關問題