Nginx+Lua+Redis配置

  想在Nginx上开发具有这样功能的一个转发模块,外部转发策略控制服务器将一些指定的URL发送给数据库,Nginx读取数据库中的URL列表,将列表指定的这些URL转发到特定的缓存代理服务器上,其他非数据库URL列表中的URL请求直接通过Nginx转发到出口网关上。实际上以上的功能就是很多网站利用Nginx做负载均衡时的实现的七层转发功能,不太一样的是,我想加一个外部的转发策略控制服务器将一些实时的URL列表发送给Nginx,这样如果这些URL信息是根据大数据处理结果统计出的最高热点访问URL,就可以在Nginx上实现基于内容热度的七层转发。

  以上是后续要完成的目标,看了看网上的资料,目前有这方面的类似模块,就是Nginx+HttpLuaModule+Redis的实现方式,Nginx不用多说是服务器,HttpLuaModule是由淘宝的工程师清无(王晓哲)和春来(章亦春)所开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力。Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,说简单了就是内存数据库,效率高。这三个模块组合在一起的基本流程是url请求nginx服务器,然后lua查询redis,返回数据,这种方式依然保持高并发。废话不多说了下面将配置方式,中间遇到了各种坑,不过幸好最后都成功解决。

  方式一:

  直接安装OpenResty,OpenResty是Nginx打包装好了各种模块,包括Lua等等。具体可以看官方主页:

  主页:http://openresty.org/

  但是我没有选择这种傻瓜,没有困难制造困难也要上,下面是手动配置Nginx+Lua+Redis步骤。

  方式二:

  1.下载Nginx源码

  http://nginx.org/en/download.html

  下载和解压都在目录 /home/zjf/ 下进行的

  cd /home/zjf

  tar -zxvf nginx-1.6.2

  解压后出现以下目录

  /home/zjf/nginx-1.6.2

  先不编译Nginx,接着下载其他需要的模块

  2.配置HttpLuaModule模块

  2.1 下载并编译安装LuaJIT 2.0 

HttpLuaModule模块需要LuaJIT 2.0(推荐LuaJIT-2.1)或者Lua 5.1(Lua 5.2不支持)

     下载链接:http://luajit.org/download.html

     下载文件:LuaJIT-2.0.3.tar.gz

     解压到:/home/zjf/luajit-2.0.3/

     编译并安装: cd /home/zjf/luajit-2.0.3/

            make

            sudo make install

2.2 下载ngx_devel_kit

     下载链接:https://github.com/simpl/ngx_devel_kit/tags

     下载文件:ngx_devel_kit-0.2.19.tar.gz

     解压到:/home/zjf/ngx_devel_kit-0.2.19

  2.3 下载ngx_lua

   下载链接:https://github.com/openresty/lua-nginx-module/tags

      下载文件: lua-nginx-module-0.9.13rc1.tar.gz

      解压到:/home/zjf/lua-nginx-module-0.9.13rc1

2.4 将以上模块和Nginx一起编译 

      第一次编译的时候出现了error:

      error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

     参考了http://blog.csdn.net/vboy1010/article/details/7868645的解决方法,我用的是在编译前导入环境变量的方法

   export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

      或者在编译时加入选项 --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" 如下

 # tell nginx's build system where to find LuaJIT 2.0:
    export LUAJIT_LIB=/usr/local/include/luajit-2.0/
    export LUAJIT_INC=/usr/local/lib/

#configure Nginx ./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" --prefix=/usr/local/nginx/ --add-module=/home/zjf/ngx_devel_kit-0.2.19/ --add-module=/home/zjf/lua-nginx-module-0.9.13rc1/ make -j2 make install

  2.5 测试Nginx+HttpLuaModule模块

     经过以上的步骤Nginx的HttpLuaModule模块就配置完成了,在浏览器输入localhost地址会出现Nginx服务器的欢迎界面,表示Nginx正常工作,如果不能正常工作,考虑是不是有其他已安装的服务器占用了端口。

     编辑/usr/local/nginx/conf/nginx.conf 文件看到server{...}内会有如下默认配置,这个是欢迎网页的url配置。

        location / {
               root   html;
               index  index.html index.htm;
          }

我们再加入如下配置:

     location /lua {  
              default_type 'text/plain';  
              content_by_lua 'ngx.say("hello, lua")';  
      }  

     然后访问localhose/lua,返回字符hello,lua,则证明Lua模块配置成功,Nginx成功的使用Lua输出了字符串hello,lua。

    3.安装Redis数据库

     redis安装很简单,下载-解压-安装即可,下载地址 http://www.redis.io/download

     tar -zxvf redis-2.8.6.tar.gz

     cd /home/zjf/redis-2.8.6

     make

sudo make install

    4.安装lua-resty-redis-master

     lua-resty-redis-masters是Lua client driver for the ngx_lua based on the cosocket API,也就是Nginx通过Lua操作Redis数据库的驱动模块。

     下载地址:https://github.com/openresty/lua-resty-redis

     解压后的目录为:/home/zjf/lua-resty-redis-master

     配置nginx.conf文件

     在http{...}段内加入以下路径,让nginx可以根据刚才的解压目录找到redis.lua模块驱动文件,目录不同可以根据情况修改,只要找到redis.lua即可。

 lua_package_path "/home/zjf/lua-resty-redis-master/lib/resty/redis.lua;;";

     然后加入一个新的location,用/test的URL来测试该模块是否配置成功。

    location /test {
        default_type 'text/plain';
            content_by_lua '
                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeout(1000) -- 1 sec

                -- or connect to a unix domain socket file listened
                -- by a redis server:
                --     local ok, err = red:connect("unix:/path/to/redis.sock")

                local ok, err = red:connect("127.0.0.1", 6379)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

                ok, err = red:set("dog", "an animal")
                if not ok then
                    ngx.say("failed to set dog: ", err)
                    return
                end

                ngx.say("set result: ", ok)

                local res, err = red: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)

                red:init_pipeline()
                red:set("cat", "Marry")
                red:set("horse", "Bob")
                red:get("cat")
                red:get("horse")
                local results, err = red:commit_pipeline()
                if not results then
                    ngx.say("failed to commit the pipelined requests: ", err)
                    return
                end

                for i, res in ipairs(results) do
                    if type(res) == "table" then
                        if not res[1] then
                            ngx.say("failed to run command ", i, ": ", res[2])
                        else
                            -- process the table value
                        end
                    else
                        -- process the scalar value
                    end
                end

                -- put it into the connection pool of size 100,
                -- with 10 seconds max idle time
                local ok, err = red:set_keepalive(10000, 100)
                if not ok then
                    ngx.say("failed to set keepalive: ", err)
                    return
                end

                -- or just close the connection right away:
                -- local ok, err = red:close()
                -- if not ok then
                --     ngx.say("failed to close: ", err)
                --     return
                -- end
            ';
        }

      访问localhost/test若配置成功,则显示

set result: OK
dog: an animal

      注意,要启动Redis数据库服务,否则后显示failed to connect : connection refused

      5.经过以上步骤,Nginx+Lua+Redis的所有配置就完成了,实现了Nginx根据Url查询Redis返回相应内容的功能,可以进行相应的二次开发。