nginx结合lua

通过给nginx添加扩展可以将让nginx结合lua一起工作,实现应用层防火墙

1、编译安装lua

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 install PREFIX=/usr/local/LuaJIT
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
加载lua库,加入到ld.so.conf文件
echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf.d/lua.conf
ldconfig

2、ngx_devel_kit和lua-nginx-module

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
tar -zxvf v0.10.9rc7.tar.gz

3、重新编译编译Nginx

wget http://nginx.org/download/nginx-1.12.1.tar.gz
执行解压,后按照如下方式编译:
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--http-client-body-temp-path=/usr/local/nginx/client_body_temp \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \
--http-proxy-temp-path=/usr/local/nginx/proxy_temp \
--without-poll_module \
--without-select_module \
--with-http_ssl_module \
--add-module=/usr/local/src/software/ngx_devel_kit-0.3.0 \
--add-module=/usr/local/src/software/lua-nginx-module-0.10.9rc7
make 
make install

4、测试lua运行
在nginx虚拟主机中添加如下配置查看输出

    location /lua {
        default_type 'text/plain';
        content_by_lua '
            ClientIP = ngx.var.remote_addr
            UserAgent = ngx.var.http_user_agent
            Headers = ngx.req.get_headers()
            Header = ngx.req.raw_header()
            Url = ngx.var.request_uri    
            ArgsString = tostring(ngx.req.get_uri_args())
            Args = ngx.req.get_uri_args()

            ngx.say("IP:",ClientIP)
            ngx.say("HEADER:",Header)
            ngx.say("UserAgent:", UserAgent)
            ngx.say("Url:", Url)
            ngx.say("------ args --------")
            ngx.say("Args:", ArgsString)
            for k,v in pairs(Args) do
                ngx.say(k, ": ", v)
            end 
            ngx.say("------ headers ------")
            for k, v in pairs(Headers) do
                ngx.say(k,": ",v)
            end 
        ';  
    }

显示如下:

 

 

lua

发表评论