Nginx + Lua + redis (one) (EXT)

Source: Internet
Author: User

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?
    1. 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?
    1. 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?
    1. Lua_code_cache off;

In the Nginx configuration file, add a location:

[Plain]View PlainCopyprint?
    1. Location/lua {
    2. Content_by_lua_file/data/www/lua/test.lua;
    3. }

Note: introduction of Test.lua script file

Lua script file:Test.lua.

[Plain]View PlainCopyprint?
  1. Local Redis = require "Resty.redis"
  2. Local cache = Redis.new ()
  3. Local OK, err = cache.connect (cache, ' 127.0.0.1 ', ' 6379 ')
  4. Cache:set_timeout (60000)
  5. If not OK then
  6. Ngx.say ("Failed to connect:", err)
  7. Return
  8. End
  9. Res, err = Cache:set ("Dog", "an Aniaml")
  10. If not OK then
  11. Ngx.say ("Failed to set dog:", err)
  12. Return
  13. End
  14. Ngx.say ("Set Result:", res)
  15. Local res, err = Cache:get ("dog")
  16. If not res then
  17. Ngx.say ("Failed to get dog:", err)
  18. Return
  19. End
  20. if res = = Ngx.null Then
  21. Ngx.say ("Dog not found.")
  22. Return
  23. End
  24. Ngx.say ("Dog:", res)
  25. Local OK, err = Cache:close ()
  26. If not OK then
  27. Ngx.say ("Failed to close:", err)
  28. Return
  29. End


The test results are as follows:

[Plain]View PlainCopyprint?
      1. [Email protected] conf]# Curl Http://localhost/lua
      2. Set Result:ok
      3. Dog:an Aniaml

http://blog.csdn.net/vboy1010/article/details/7892120

Nginx + Lua + redis (one) (EXT)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.