Nginx实现WAF

这个我们说下,基于Nginx的WAF,基本上分两大类,一种是基于第三方插件来实现,一种是使用lua语言通过Nginx-Lua整合到一起,就性能而言,我们推荐选择轻量级的lua来实现。现在我们只是举例说明下。

说明

这里我们推荐各位去学习下lua语言,一种简单高效的语言

 

组件

  • jemalloc-4.04.4.tar.bz

  • LuaJIT-2.0.4.tar.gz

  • openresty-1.9.7.4.tar.gz

  • lua-nginx-module-master.zip

  • ngx_devel_kit-0.2.19.tar.gz

  • nginx-1.8.1.tar

  • ngx_lua_waf-master.zip

依赖

基础编译

  • 基础编译

  • gcc

  • make

  • nginx_upstream_check_module

  • patch

  • openresty

  • perl

  • pcre-devel

  • readline-devel

  • openssl-devel

安装依赖

Shell>#yum -y install gcc make unzip openssl-devel pcre-devel readline-devel make

安装jemalloc

Shell>#tar xf jemalloc-4.0.4.tar.bz2

Shell>#cd jemalloc-4.0.4

Shell>#./configure

Shell>#make

Shell>#make install

shell>#echo /usr/local/lib > /etc/ld.so.conf.d/local.conf

shell>#ldconfig

安装LuaJIT

Shell>#tar xf LuaJIT-2.0.4.tar.gz

Shell>#cd LuaJIT-2.0.4

Shell>#make

Shell>#make install

设置环境变量

Shell>#vi/etc/profie

添加如下内容


我们在窗口中也运行下这个命令,方面使其生效,方便我们继续下去

安装 openresty

Shell>#tar xf openresty-1.9.7.4.tar.gz

Shell>#cd openresty-1.9.7.4

Shell>#yum -y install perl

Shell>#./configure--with-luajit --with-pcre

Shell>#gmake

Shell>#gmake install

解压缩nginx模块

Shell>#unzip lua-nginx-module-master.zip

Shell>#tarxf  ngx_devel_kit-0.2.19.tar.gz

安装nignx-1.8.1

Shell>#tar xf nginx-1.8.1.tar.gz

Shell>#./configure

./configure  \

--prefix=/etc/nginx  \

--sbin-path=/usr/sbin/nginx  \

--conf-path=/etc/nginx/nginx.conf  \

--error-log-path=/var/log/nginx/error.log  \

--http-log-path=/var/log/nginx/access.log  \

--pid-path=/var/run/nginx.pid  \

--lock-path=/var/run/nginx.lock  \

--http-client-body-temp-path=/var/cache/nginx/client_temp  \

--http-proxy-temp-path=/var/cache/nginx/proxy_temp  \

--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp  \

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp  \

--http-scgi-temp-path=/var/cache/nginx/scgi_temp  \

--user=nginx  --group=nginx \

--with-http_ssl_module  \

--with-http_realip_module  \

--with-http_gunzip_module  \

--with-http_gzip_static_module  \

--with-http_stub_status_module  \

--with-file-aio  \

--with-ld-opt=-ljemalloc  \

--with-http_spdy_module  \

--with-ld-opt="-Wl,-rpath,/usr/local/lib/"  \

--add-module=/root/ngx_devel_kit-0.2.19/  \

--add-module=/root/lua-nginx-module-0.10.2/

Shell>#make –j2

Shell>#make install

Shell>#cd /etc/nginx/

Shell>#unzip ngx_lua_waf-master.zip

Shell>#mv ngx_lua_waf waf

Shell>#vi nginx.conf

添加如下内容

user  nginx;

worker_processes  1;

events  {

    worker_connections  1024;

}

http  {

       lua_package_path  "/etc/nginx/waf/?.lua";

     lua_shared_dict limit 100m;

     init_by_lua_file /etc/nginx/waf/init.lua;

     access_by_lua_file /etc/nginx/waf/waf.lua;

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

然后重启nginx,我们的waf就开始运行了。