Use the Lua scripting language to manipulate Redis.
Because a lot of LUA code is written in Nginx, it makes the configuration file cumbersome, so use Content_by_lua_file to introduce Lua script files.
To use Content_by_lua_file, you need to install the Nginx_lua_module module.
Installation Introduction, Bash here:nginx_lua_module
The great god Zhang Yichun provides a handy development package, as follows:
[Plain]View PlainCopyprint?
- git clone https://github.com/agentzh/lua-resty-redis.git
In the package, there is a Lib directory that copies the files and subdirectories in the Lib directory to the directory/data/www/lua
In the Nginx configuration file, you need to add a line of code to introduce Redis.lua.
Note: Add the HTTP segment.
[Plain]View PlainCopyprint?
- Lua_package_path "/data/www/lua/?" LUA;; ";
In order to make the Lua script changes effective in time, you need to add a line of code, as follows:
Note: in the server section, add code, if not add this code or set to ON, you need to restart Nginx.
[Plain]View PlainCopyprint?
- Lua_code_cache off;
In the Nginx configuration file, add a location:
[Plain]View PlainCopyprint?
- Location/lua {
- Content_by_lua_file/data/www/lua/test.lua;
- }
Note: introduction of Test.lua script file
Lua script file:Test.lua.
[Plain]View PlainCopyprint?
- Local Redis = require "Resty.redis"
- Local cache = Redis.new ()
- Local OK, err = cache.connect (cache, ' 127.0.0.1 ', ' 6379 ')
- Cache:set_timeout (60000)
- If not OK then
- Ngx.say ("Failed to connect:", err)
- Return
- End
- Res, err = Cache:set ("Dog", "an Aniaml")
- If not OK then
- Ngx.say ("Failed to set dog:", err)
- Return
- End
- Ngx.say ("Set Result:", res)
- Local res, err = Cache:get ("dog")
- If not res then
- Ngx.say ("Failed to get dog:", err)
- Return
- End
- if res = = Ngx.null Then
- Ngx.say ("Dog not found.")
- Return
- End
- Ngx.say ("Dog:", res)
- Local OK, err = Cache:close ()
- If not OK then
- Ngx.say ("Failed to close:", err)
- Return
- End
The test results are as follows:
[Plain]View PlainCopyprint?
- [Email protected] conf]# Curl Http://localhost/lua
- Set Result:ok
- Dog:an Aniaml
http://blog.csdn.net/vboy1010/article/details/7892120
Nginx + Lua + redis (one) (EXT)