Nginx embedded LUA script, combined with Redis

Source: Internet
Author: User
Tags install redis
0x00 Nginx Embedded LUA scripts have the following features:

    • 20k of concurrent connections

    • Lua scripts can function at different levels of the Nignx 11 levels, extending Ngnix functionality

    • Lua is extremely fast (register instructions)

0X01 Application Scenario

    • Request filtering processing on the Web server side (e.g. WAF, Anti cc, etc.)

0x02 Simple configuration process

    1. Test environment Ubuntu Server 14.04.2 LTS

    2. Several modules to download (note the installation sequence 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. are not found because Luajit Lib and Inc are not configured in the environment variable

This configuration is required (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 there is a situation that cannot be started, the service can view the Tail/var/log/syslog view error

If it is nginx cannot start can view Tail/var/cache/nginx/error.log

If you have generated an Nginx bin file, you can use NGINX-V to see if the configuration file is correct


If the module is missing:

PCRE

sudo apt-get install libpcre3 Libpcre3-dev

Zlib

sudo apt-get install Zlib1g-dev

Openssl

sudo apt-get install Libssl-dev

PS: In particular, please note that the following nginx version do not download the latest, may not support the above modules interface, I use the Nginx 1.7.4


Where 0x02 installation instructions are installed, there is no detail

After the installation of 0x04

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

    1. Add LUA Code

Re-load Nginx configuration

Sudo/etc/nginx/sbin/nginx-s Reload

Effect:

2. Add LUA files:

Add two Lua_package_path,lua_code_cache (in order to not keep LUA cache, easy to debug, need to open in the actual project)

Directory of the overall LUA files (note that the files in the Lua folder are created next):

/etc/nginx/lua/hello.lua

/etc/nginx/lua/hello_redis.lua

/etc/nginx/lua/redis.lua


nginx.conf file Add:



Hello.lua File Contents:

Ngx.header.content_type = "Text/plain";

Ngx.say ("Say Hello from Hello.lua");

All added location codes:


Then re-load Nginx to see the effect.

3. Using Redis (third newly added redis):


The prerequisite is that the machine is already redis-server, and the installation on Ubuntu is sudo apt-get install redis-server


Hello_redis.lua content:

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


Effect:

0x05 so far, the simple one in nginx embedded LUA and operation of the Redis process has been completed, in the configuration may have a lot of small problems, but do not give up, persist, believe you will succeed.

0xFF Additional information:

Http://wiki.nginx.org/HttpLuaModule

http://openresty.org/(first Chinese to complete nginx embedded LUA)

http://tengine.taobao.org/

Reprint please indicate the source (personal forum): http://www.byteway.net/thread-index-fid-4-tid-316.htm


The above describes the Nginx embedded Lua script, combined with Redis use, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.