Centos 7下nginx+lua+redis的访问控制

        目前nginx作为http服务器的使用越来越广泛,其采用的模块化方式,为开发带来便捷。因此也是一款轻量级的服务器。目前主要使用的是其反向代理、负载均衡功能和前后端分离等功能。但是还有很多功能可以为我们的业务带来简洁和使用。本文入门讲解关于使用nginx+lua+redis实现访问控制,包括防止接口攻击,对前端做限制,也可以对于一些静态资源的访问权限控制,还可以对身份识别控制等。服务器采用的是centos 7,下面是详细的操作步骤:


     1、首先安装nginx:

      a、先到nginx官网下载nginx的tar包,网址:http://nginx.org/en/download.html

      b、安装依赖包(依赖包有点多,我们采用yum的方式来安装)

            yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

      c、上传nginx到/home目录下,再解压nginx:

            tar -zxvf nginx-1.13.5.tar.gz

      d、进入nginx目录,并安装:

            cd nginx-1.13.5

                    ./configure

            make

            make install

     e、运行nginx

          ./usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

     2、安装redis:

         a、首先下载redis的tar包,redis网址:https://redis.io/download

         b、上传redis到/home目录下,解压redis:

              tar -zxvf redis-3.2.0.tar.gz

        c、进入redis-3.2.0目录,并安装:

             cd redis-3.2.0

             make && make install

        d、启动redis,进入src目录:

             cd src

             ./redis-server ../redis.conf

        注意修改redis的配置文件,将bind 127.0.0.1注释掉,以及改daemonize yes,修改protected-mode no

       3、由于本机在安装centos 7已经安装lua脚本,可以通过lua命令查看是否已经安装,如下图:

            

          如果未安装可以参考:http://blog.csdn.net/yangxuan0261/article/details/52093496


           4、将lua集成到nginx中:

         a、下载ngx_openresty-1.7.7.2.tar.gz并解压

              wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz

             tar -xzvf ngx_openresty-1.7.7.2.tar.gz 

          b、ngx_openresty-1.7.7.2/bundle目录里存放着nginx核心和很多第三方模块,比如有我们需要的Lua和  LuaJIT,安装LuaJIT:

           cd bundle/LuaJIT-2.1-20150120

          make clean && make && make install

          c、下载ngx_cache_purge模块,该模块用于清理nginx缓存

              cd /home/ngx_openresty-1.7.7.2/bundle 

              wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz

             tar -xvf 2.3.tar.gz

          d、下载nginx_upstream_check_module模块,该模块用于ustream健康检查

               cd /home/ngx_openresty-1.7.7.2/bundle
              wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
              tar -xvf v0.3.0.tar.gz

          e、安装ngx_openresty

             cd /home/ngx_openresty-1.7.7.2
            ./configure --prefix=/usr/local --with-http_realip_module  --with-pcre  --with-luajit --add-module=./bundle    /ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
            make && make install

                  

            --with***                安装一些内置/集成的模块

            --with-http_realip_module  取用户真实ip模块

            -with-pcre               Perl兼容的达式模块

            --with-luajit              集成luajit模块

 

            --add-module            添加自定义的第三方模块,如此次的ngx_che_purge


          5、新建lua脚本,access_limit.lua

             

             
ngx.req.read_body()
local redis  = require "resty.redis"
local red = redis.new()
red.connect(red,'127.0.0.1','6379')

local myIP = ngx.req.get_headers()["X-Real-IP"]
if myIP == nil then
        myIP = ngx.req.get_headers()["x_forwarded_for"]
end
if myIP == nil then
        myIP = ngx.var.remote_addr
end

if ngx.re.match(ngx.var.uri,"^(/myapi/).*$") then
        local method = ngx.var.request_method
        if method == 'GET' then
                local  args = ngx.req.get_uri_args()
                --ngx.log(args,1,)
                local hasIP = red:get('black.ip')
                local hasIMSI = red:get('black.imsi')
                local  hasTEL  = red:get('black.tel')
                ngx.log(ngx.ERR,'string',myIP)
                ngx.log(ngx.ERR,'string',hasIMSI)
                ngx.log(ngx.ERR,'string',args.imsi)
                if hasIP==myIP or hasIMSI==args.imsi or hasTEL==args.tel then
                        ngx.exit(ngx.HTTP_FORBIDDEN)
                end
        else
        --      ngx.say("hello world");
                ngx.exit(ngx.HTTP_FORBIDDEN)
        end
end

               6、nginx.conf配置lua,在http中引入如下:

             #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/local下找  
             lua_package_path "/usr/local/lualib/?.lua;;";  #lua 模块  
             lua_package_cpath "/usr/local/lualib/?.so;;";  #c模块

            在server中的配置如下:

             location / {
                   root   html;
                   index  index.html index.htm;
                   access_by_lua_file /usr/local/nginx/lua_test/access_limit.lua;
             }

           7、重启nginx

               ./usr/local/nginx/sbin/nginx -s reload

          

          8、添加数据:

              在redis中添加一条数据如图: 


          

           上图工具是redis的GUI,可以网上下载:https://redisdesktop.com/download

           在浏览器中访问:http://192.168.182.128/myapi/?imsi=460123456789,如图,则说明已经被拦截处理:

             


到这里并未结束,只是刚开始。

      更多精彩参见:

          https://github.com/search?utf8=%E2%9C%93&q=lua+resty   https://github.com/openresty/lua-resty-redis       

         


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值