Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Dynamically extend Nginx to support Lua function

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

1. Lua description:

Lua is a simple, lightweight, extensible scripting language, but also claimed to be the highest performance scripting language, used in many places that need performance, such as: game scripts, nginx,wireshark scripts, when you put his source code down to compile, you will find that the interpreter is actually less than 200k, very abnormal. Many applications use Lua as their embedded scripting language to achieve configurability and extensibility.

2. Experimental environment:

192.168.1.194 server (centos 6.5 final version)

Nginx version: nginx-1.6.1

3. Installation configuration:

Assuming that nginx is installed (nginx is easy to install and there are many online cases), let's start dynamically compiling the extension nginx to support lua scripts.

Download and install luajit (lua just-in-time compiler)

# wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz # tar zxvf LuaJIT-2.0.2.tar.gz# cd LuaJIT-2.0.2# make # make install

Download the lua-nginx-module module and ngx_devel_kit (nginx Development Kit) to the nginx sibling directory and extract it

# wget https://codeload.github.com/openresty/lua-nginx-module/zip/v0.10.1-o lua-nginx-module-0.10.1.zip#unzip lua-nginx-module-0.10.1.zip#wget-o ngx_devel_kit-0.2.18.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz # tar zxvf ngx_devel_kit-0.2.18.tar.gz

Nginx-V can view the version of nginx, as well as previously compiled projects.

# / usr/local/apps/nginx/sbin/nginx-Vnginx version: nginx/1.6.1built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) TLS SNI support enabledconfigure arguments:-- prefix=/usr/local/apps/nginx/-- user=nginx-- group=nginx-- with-http_stub_status_module-- with-http_gzip_static_module-- with-http_flv_module-- with-http_mp4_module-- with-pcre=/ Home/workspace/auto_deploy/software/nginx/pcre-8.30-with-openssl=/home/workspace/auto_deploy/software/nginx/openssl-1.0.1i-add-module=../gnosek-nginx-upstream-fair-a18b409/-add-module=../ngx_cache_purge-2.1-with-http_ssl_module

There are two ways for nginx to support lua:

Method a, using luajit just-in-time compiler

# export LUAJIT_LIB=/usr/local/lib # export LUAJIT_INC=/usr/local/include/luajit-2.0 # / configure-- prefix=/usr/local/apps/nginx/-- user=nginx-- group=nginx-- with-http_stub_status_module-- with-http_gzip_static_module-- with-http_flv_module-- with-http_mp4_module-- with-pcre=/home/workspace/auto_deploy/software/nginx/pcre-8.30-- with-openssl= / home/workspace/auto_deploy/software/nginx/openssl-1.0.1i-add-module=../gnosek-nginx-upstream-fair-a18b409/-- add-module=../ngx_cache_purge-2.1-- with-http_ssl_module-- add-module=../lua-nginx-module-0.10.1/-- add-module=../ngx_devel_kit-0.2.18/# make-j 4 # make install # echo'/ usr/ Local/lib' > > / etc/ld.so.conf.d/lua.conf # ldconfig

Method b, use the lua compiler

# export LUA_LIB=/usr/lib64 # export LUA_INC=/usr/include # / configure-- prefix=/usr/local/apps/nginx/-- user=nginx-- group=nginx-- with-http_stub_status_module-- with-http_gzip_static_module-- with-http_flv_module-- with-http_mp4_module-- with-pcre=/home/workspace/auto_deploy/software/nginx/pcre-8.30-- with-openssl=/home/workspace/auto_deploy/software / nginx/openssl-1.0.1i-add-module=../gnosek-nginx-upstream-fair-a18b409/-add-module=../ngx_cache_purge-2.1-with-http_ssl_module-add-module=../lua-nginx-module-0.10.1/-add-module=../ngx_devel_kit-0.2.18/# make-j 4 # make install

Note: there are two ways to make nginx support lua, one is to use the luajit just-in-time compiler, and the other is to use the lua compiler. Luajit is recommended because of its high efficiency.

Once compiled and installed, nginx can support lua scripts.

Then use nginx-V to look at the modules supported by nginx.

# / usr/local/rktsapps/nginx/sbin/nginx-Vnginx version: nginx/1.6.1built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) TLS SNI support enabledconfigure arguments:-- prefix=/usr/local/apps/nginx/-- user=nginx-- group=nginx-- with-http_stub_status_module-- with-http_gzip_static_module-- with-http_flv_module-- with-http_mp4_module-- with-pcre=/ Home/workspace/auto_deploy/software/nginx/pcre-8.30-with-openssl=/home/workspace/auto_deploy/software/nginx/openssl-1.0.1i-add-module=../gnosek-nginx-upstream-fair-a18b409/-add-module=../ngx_cache_purge-2.1-with-http_ssl_module-add-module=../lua-nginx-module-0.10.1/-add-module=../ngx_devel_kit-0.2.18/

You can see that our two new modules have been compiled into nginx.

Verify configuration instructions and output

Modify the nginx.conf configuration file by adding the following directive:

Location / {content_by_lua 'ngx.say ("hello world!")';}

Restart nginx and test with curl

# curl-I localhost

The result is:

HTTP/1.1 200 OK Server: nginx/1.4.1 Date: Tue, 24 Sep 2013 23:23:58 GMT Content-Type: application/octet-streamTransfer-Encoding: chunked Connection: keep-alive hello world!

5. Problems encountered in installation:

Error prompt:

Make [1]: * * [/ apps/lib/openssl-1.0.1i/.openssl/include/openssl/ssl.h] Error 2

Modify the contents of a file in nginx source code.

# vim nginx-1.6.1/auto/lib/openssl/confCORE_INCS= "$CORE_INCS $OPENSSL/.openssl/include" CORE_DEPS= "$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h" CORE_LIBS= "$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a" CORE_LIBS= "$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a" CORE_LIBS= "$CORE_LIBS $NGX_LIBDL"

Replace with

CORE_INCS= "$CORE_INCS $OPENSSL/include" CORE_DEPS= "$CORE_DEPS $OPENSSL/include/openssl/ssl.h" CORE_LIBS= "$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a" CORE_LIBS= "$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a" CORE_LIBS= "$CORE_LIBS $NGX_LIBDL"

Just recompile and install!

Recommended reading:

Play Nginx: extend the Nginx function with Lua

Let Nginx support Lua

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report