Nginx 内嵌lua脚本,结合Redis使用

WBOY
Freigeben: 2016-08-08 09:23:16
Original
1174 Leute haben es durchsucht

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路径问题)

  • Nginx 1.7.4

  • LuaJIT-2.0.4(A Just-In-Time Compiler for Lua)

  • ngx_devel_kit( Nginx Development Kit)

  • echo-nginx-module( more shell-style goodies to Nginx config file)

  • lua-nginx-module(Embed the Power of Lua into Nginx)

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-/* /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


以上就介绍了Nginx 内嵌lua脚本,结合Redis使用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!