Nginx embedded lua script, used in conjunction with Redis

WBOY
Release: 2016-08-08 09:23:16
Original
1196 people have browsed it

0x00 Nginx embedded Lua script has the following features:

  • 20k concurrent connections

  • Lua script can function at different levels of Nignx 11 levels, extending Ngnix functions

  • Lua is extremely fast ( Register instructions)

0x01 Application scenario

  • Request filtering processing on the web server side (such as WAF, Anti CC, etc.)

0x02 Simple configuration process

  1. Test environment Ubuntu Server 14.04.2 LTS

  2. Several modules that need to be downloaded (pay attention to the installation order and export path issues)

  • 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 Possible problems, lua.h, etc. cannot be found, because the lib and inc of luaJIT are not configured in the environment variables

You need to configure it like this (your actual local path) :

export LUAJIT_LIB=/usr/lib/lua

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

cp /usr/local/include/luajit-/* /usr/local/ include/


If the service cannot start, you can check tail /var/log/syslog to check the error

If nginx cannot start, you can check tail /var/cache/nginx/error.log

If If the nginx bin file has been generated, you can use nginx -V to check whether the configuration file is correct.

sudo apt- get install zlib1g-dev


openssl

sudo apt-get install libssl-dev

ps: In particular, please be careful not to download the latest version of Nginx. It may not support the above module interfaces. I I am using Nginx 1.7.4

The installation steps of 0x02 have installation instructions, so I won’t go into details here

0x04 After installation

Modify the nginx.conf file (default path /etc/nginx/ nginx.conf):

Add lua code


Reload nginx configuration


sudo /etc/nginx/sbin/nginx -s reload
  1. Effect :

2. Add lua files:

Add two lua_package_path, lua_code_cache (in order not to retain the lua cache and facilitate debugging, it needs to be opened in the actual project)


The directory of the overall lua file (note that the files in the lua folder are next New):

/etc/nginx/lua/hello.lua

/etc/nginx/lua/hello_redis.lua

/etc/nginx/lua/redis.lua


nginx.conf file addition:


hello.lua file content:


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

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

All added location codes:



Then reload nginx to see the effect.

3. Use redis (the third newly added redis):



The premise is that there is already redis-server on the machine, and the installation on Ubuntu is

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教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template