Nginx and lua

Source: Internet
Author: User

Nginx and lua

As an embedded script, lua is currently the fastest among all script engines. It is compiled by standard C and can be compiled and run in almost all operating systems and platforms.

Why should we use nginx + lua?

Let's compare nginx + lua and nginx + php:

Process Communication is required between nginx and php, so the performance overhead is high. lua is embedded in the nginx process and does not need to have two sets of processes to work independently, therefore, the interface has a decisive advantage. In addition, communication between threads requires a lot of deserialization and serialization work, the additional situations brought about by the last two sets of processes are more processes and more switching overhead. Therefore, nginx + lua performance is much better than nginx + php.

However, nginx + lua also has a disadvantage, that is, the surrounding modules of nginx + lua are quite imperfect. If the requirements for concurrency performance are not very high, php is more suitable.

The purpose of applying nginx + lua is to develop nginx-based business logic through lua, because we need high concurrency and it is suitable for the interface layer.

Environment setup:

1. Install luajit

Luajit uses jit compilation to directly compile the lua script into a machine code and run it by the cpu.

 

wget http://luajit.org/download/LuaJIT-2.0.4.tar.gztar -zxvf LuaJIT-2.0.4.tar.gzcd LuaJIT-2.0.4make install prefix=/usr/local/luajit

 

Add environment variables:

 

export LUAJIT_LIB=/usr/local/luajit/libexport LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
Load class library

 

 

echo "/usr/local/luajit/lib" > /etc/ld.so.conf.d/usr_local_luajit_lib.confldconfig
Otherwise, the following error occurs when operating nginx:

 

 

[root@ nginx]# ./nginx -V./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory


2. Install ngx_devel_kit and lua-nginx-module

 

The nginx_devel_kit module is a module that expands the core functions of the nginx server. It provides functions and macros to process some basic tasks and reduces the amount of code developed by third-party modules.

The lua-nginx-module embeds the lua language into the nginx configuration, which improves the nginx capability.

 

# Download ngx_devel_kit and lua-nginx-modulewget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gzwget https://github.com/openresty/lua-nginx-module/archive/v0.9.16rc1.tar.gztar-zxvf v0.2.19.tar.gz tar-zxvf v0.9.16rc1.tar.gz # compile and install nginxcd/usr/local/nginx. /configure -- prefix =/usr/local/nginx -- add-module =/usr/local/src/lua/lua-nginx-module-0.9.16rc1 -- add-module =/usr/local/src/lua/ install ngx_devel_kit-0.2.19makemake

3. Test

 

Add the following to the nginx configuration file:

 

        location /test {                content_by_lua '                                                if jit then                                ngx.say(jit.version)                        else                                ngx.say(_VERSION)                        end                ';        }
./Check nginx-s reload:

 

 

 curl 192.168.3.126/testLuaJIT 2.0.4
The above indicates that the nginx + lua environment is successfully installed.

 

Note: To view the information in the browser, you must add it to content_by_lua.Ngx. header. content_type = "text/plain ";Otherwise, the page is not displayed.

In addition, to facilitate nginx to call related lua modules, We need to customize the lua module path in the nginx configuration file, such:

 

package.path = "/usr/local/nginx/lualib/?.lua;/usr/local/nginx/lualib/captcha/?.lua;"package.cpath = "/usr/local/nginx/lualib/?.so;/usr/local/nginx/lualib/captcha/?.so;"

 

Now, let's talk about the initial environment setup. We will introduce nginx + lua-related applications in the future.

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.