使用带有redis的nginx读取键值对

时间:2017-12-06 10:17:44

标签: nginx redis

我是nginx和redis的新手,无法从nginx连接redis来读取redis键值

我看到redis的大多数文档都是指在nginx上不再活跃的旧模块

请帮助一个简单的例子

1 个答案:

答案 0 :(得分:2)

我正在使用openresty和redis读取键值对。 这是我的conf

server {
    listen       80;
    server_name  localhost;

  #GET http://localhost/get?key=key
  location /get {
      default_type "text/plain";
        content_by_lua_block {
        local redis_info = {
            host = "127.0.0.1",
            port = 6379, 
            db = 0
        }
        local redis = require "resty.redis"
        local red = redis:new()
        red:set_timeout(1000)

        local ok, err = red:connect(redis_info.host, redis_info.port)
        if not ok then
            ngx.say("failed to connect: ", err)
            return
        end
        ok, err = red:select(redis_info.db)

        local value = red:get(ngx.var.arg_key)
        ngx.say(value)
    }
  }

  #SET http://localhost/set?key=key&val=value
  location /set {
      default_type "text/plain";
      set_unescape_uri $key $arg_key;
      set_unescape_uri $val $arg_val;
      redis2_query set $key $val;
      redis2_pass 127.0.0.1:6379;
  }
}

您可以使用nginx please look at the document here.

执行相同的操作