zoukankan      html  css  js  c++  java
  • Nginx 内嵌lua脚本,结合Redis使用

    0x00 Nginx 内嵌Lua脚本有下面特点:

    • 20k个并发连接

    • Lua脚本能够在Nignx 11个层次的不同层次发挥作用,扩展Ngnix功能

    • Lua速度极快(寄存器指令)


    0x01 应用场景

    • 在web server端做请求过滤处理(如:WAF、Anti CC等)


    0x02 简单配置过程

    1. 測试环境Ubuntu Server 14.04.2 LTS

    2. 几个须要下载的模块(注意安装顺序和export路径问题)


    0x03 可能存在的问题。找不到 lua.h 等,是由于luaJIT的lib和inc没有配置在环境变量中

        须要这样配置(你实际的本地路径):

        export LUAJIT_LIB=/usr/lib/lua

        export LUAJIT_INC=/usr/local/include/luajit-2.0

        cp /usr/local/include/luajit-<VERSION>/* /usr/local/include/

        

        假设有无法启动的情况。service 能够查看 tail /var/log/syslog  查看错误

        假设是nginx无法启动能够查看 tail /var/cache/nginx/error.log

        假设已经生成nginx bin文件 能够用 nginx -V 来查看 配置文件是否正确

        

        假设缺少一下模块:

        PCRE

        sudo apt-get install libpcre3 libpcre3-dev


        zlib

        sudo apt-get install zlib1g-dev

        

        openssl

        sudo apt-get install libssl-dev


        ps:特别说明的是。请注意下Nginx的版本号不要下载最新的,可能不支持上面那些模块接口,我用的是Nginx 1.7.4

        

        当中0x02的安装步骤都有安装说明,这里就不细说了


    0x04 安装完后

        改动nginx.conf文件 (默认路径 /etc/nginx/nginx.conf):

    1. 加入lua代码

        

        又一次load nginx 配置

        sudo /etc/nginx/sbin/nginx -s reload


        效果:

        

        2.  加入lua 文件:

        加入两个lua_package_path,lua_code_cache(为了不保留lua cache,方便调试。实际项目中须要打开)

        


        总体的lua文件的文件夹(注意lua文件夹中的文件是接下来新建的):

        /etc/nginx/lua/hello.lua

        /etc/nginx/lua/hello_redis.lua

        /etc/nginx/lua/redis.lua

        

        nginx.conf 文件加入:

        

     

        

        hello.lua文件内容:

         ngx.header.content_type = "text/plain";

         ngx.say("say hello from hello.lua");


        全部加入的location代码:

        

        

        然后又一次load nginx 看效果。


        3.使用redis(第三条新加的redis):


        

        

         前提是机器上已有redis-server, Ubuntu上安装是 sudo apt-get install redis-server

        

        hello_redis.lua 内容:

        

        local redis = require "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("hello", "redis in nginx_inline_lua")

        if not ok then

                ngx.say("failed to set hello: ", err)

                return

        end

        

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

        

        local res, err = cache:get("hello")

        if not res then

                ngx.say("failed to get hello: ", err)

                return

        end

        

        if res == ngx.null then

                ngx.say("hello not found.")

                return

        end

        

        ngx.say("hello: ", res)


        local ok, err = cache:close()

        

        if not ok then

            ngx.say("failed to close:", err)

            return

        end

        

        效果:

        


    0x05 如今为止,简单的一个在Nginx 中内嵌Lua而且操作Redis的过程已经完毕了,在配置时候可能有非常多细小的问题。可是不要放弃,坚持下去。相信你就会成功。



    0xFF 附加资料:

        http://wiki.nginx.org/HttpLuaModule

        http://openresty.org/ (最先完毕Nginx内嵌Lua的Chinese)

        http://tengine.taobao.org/


    转载请注明出处(个人论坛):http://www.byteway.net/thread-index-fid-4-tid-316.htm


  • 相关阅读:
    16个最棒的jQuery视差滚动效果教程
    16个最棒的WordPress婚纱摄影网站主题
    2013年最受欢迎的16个HTML5 WordPress主题
    16个最佳PSD文件下载网站
    16个最热门的 Android Apps 推荐下载
    前端工程师应该都了解的16个最受欢迎的CSS框架
    16个最好并且实用的jQuery插件【TheTop16.com】
    16个最受欢迎的Magento电子商务主题【TheTop16.com】
    [Nunit] System.Net.Sockets.SocketException : An existing connection was forcibly closed by the remote host
    WORD
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6724217.html
Copyright © 2011-2022 走看看