Nginx is embedded with the lua script and used in combination with Redis

Source: Internet
Author: User
: This article mainly introduces the Nginx embedded lua script, which is used in conjunction with Redis. For more information about PHP tutorials, see. 0x00 Nginx embedded Lua script has the following features:

  • 20 k concurrent connections

  • The Lua script can be used at different layers of the 11 ngnx, and the Ngnix function can be extended.

  • Lua is extremely fast (register command)

0x01 application scenarios

  • Filter requests on the web server (such as WAF and Anti CC)

0x02 simple configuration process

  1. Test Environment: Ubuntu Server 14.04.2 LTS

  2. Several modules to be downloaded (pay attention to installation sequence and export path issues)

  • Nginx 1.7.4

  • A 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 problems may exist. lua. h cannot be found because the lib and inc of luaJIT are not configured in the environment variable.

You need to configure 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 be started, you can view tail/var/log/syslog errors.

If nginx cannot be started, you can view tail/var/cache/nginx/error. log.

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


If the following modules are missing:

PCRE

Sudo apt-get install libpcre3 libpcre3-dev

Zlib

Sudo apt-get install zlib1g-dev

Openssl

Sudo apt-get install libssl-dev

Ps: note that do not download the latest Nginx version. these module interfaces may not be supported. I use Nginx 1.7.4.


The installation steps for 0x02 are described here.

0x04 after installation

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

  1. Add lua code

Re-load nginx configuration

Sudo/etc/nginx/sbin/nginx-s reload

Effect:

2. add the lua file:

Add two lua_package_path and lua_code_cache (to facilitate debugging without retaining the lua cache, you need to open it in the actual project)

The Directory of the overall lua File (note that the file in the lua folder is created next ):

/Etc/nginx/lua/hello. lua

/Etc/nginx/lua/hello_redis.lua

/Etc/nginx/lua/redis. lua


Add the nginx. conf file:



Hello. lua file content:

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

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

All added location Codes:


Load nginx again to check the effect.

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


The premise is that redis-server already exists on the machine, and sudo apt-get install redis-server is installed on Ubuntu.


Hello_redis.lua content:

Local redis = require "redis"


Local cache = redis. new ()


Local OK, err = cache. connect (cache, '127. 0.0.1 ', '123 ')


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, a simple process of embedding Lua in Nginx and operating Redis has been completed. many minor problems may occur during configuration, but don't give up and stick to it, I believe you will succeed.

0xFF additional information:

Http://wiki.nginx.org/HttpLuaModule

Http://openresty.org/(first completed Nginx embedded Lua Chinese)

Http://tengine.taobao.org/

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


The above introduces the Nginx embedded lua script, which is used in combination with Redis, including some content, and hopes to help friends who are interested in PHP tutorials.

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.