一、概述

需求:所有访问/webapi/**的请求必须是GET请求,而且根据请求参数过滤不符合规则的非法请求(黑名单),可以返回具体的http状态码,提示客户端IP被列入黑名单,遭到请求的限制
实现思路:通过在Nginx上进行访问限制,通过Lua来灵活实现业务需求,而Redis用于存储黑名单列表。

二、具体实现

1.lua代码
本例中限制规则包括(post请求,ip地址黑名单,请求参数中imsi,tel值和黑名单)

[root@git-server ~]# cat /usr/local/nginx/conf/lua/ipblacklist.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,"^(/webapi/).*$") then
    local method = ngx.var.request_method
    if method == 'GET' then
        local args = ngx.req.get_post_args()

        local hasIP = red:sismember('black.ip',myIP)
        local hasIMSI = red:sismember('black.imsi',args.imsi)
        local hasTEL = red:sismember('black.tel',args.tel)
        if hasIP==1 or hasIMSI==1 or hasTEL==1 then
            --ngx.say("This is 'Black List' request")
            ngx.exit(ngx.HTTP_FORBIDDEN)
          end
        else
            ngx.say("This is 'POST' request")
            --ngx.exit(ngx.HTTP_FORBIDDEN)
          end
         end

2.ngnx的server虚拟主机配置文件

server {
        listen       80;
        server_name  www.kjios.com kjios.com;
        index index.html index.htm index.php;
        root /data/www/www.kjios.com;
        location ~ .*\.(php|php5)?$
        {
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }

        location ~ /webapi/ {
           access_by_lua_file /usr/local/nginx/conf/lua/ipblacklist.lua;
           default_type 'text/plain';
           content_by_lua ' 
                        ngx.say("

hello lua

")'; } access_log /data/wwwlogs/kjios.com_access.log; }

从新加载nginx服务,使配置文件生效

3.在redis中添加黑名单规则数据

#redis-cli sadd black.ip '153.34.118.50'
#redis-cli sadd black.imsi '460123456789' 
#redis-cli sadd black.tel '15888888888'
[root@git-server ~]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> smembers black.ip
1) "124.207.48.234"
2) "21.95.1.233"
127.0.0.1:6379> smembers black.tel
1) "15888888888"
127.0.0.1:6379> smembers black.imsi
1) "460123456789"
127.0.0.1:6379> 

4.验证结果

[root@git-server ~]# curl -d "imsi=460123456789&tel=15800000000" "http://www.kjios.com/webapi/111"
This is 'POST' request
[root@git-server ~]# tail -3 /data/wwwlogs/kjios.com_access.log 
21.95.1.233 - - [05/Jun/2018:23:18:51 +0800] "POST /myapi/111 HTTP/1.1" 404 162 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
21.95.1.233 - - [05/Jun/2018:23:19:00 +0800] "POST /webapi/111 HTTP/1.1" 200 34 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
21.95.1.233 - - [05/Jun/2018:23:20:56 +0800] "POST /webapi/111 HTTP/1.1" 200 34 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
[root@git-server ~]# 

登录redis把家里的ip设置为黑名单,来测试
127.0.0.1:6379> sadd black.ip 211.144.7.32
(integer) 1
127.0.0.1:6379> smembers black.ip
1) "211.144.7.32"

nginx+lua+redis实现GET请求接口之黑名单(二)_第1张图片

去掉家里的ip黑名单测试

127.0.0.1:6379> srem black.ip 211.144.7.32
(integer) 1
127.0.0.1:6379> smembers black.ip
1) "124.207.48.234"
2) "21.95.1.233"
127.0.0.1:6379> 

nginx+lua+redis实现GET请求接口之黑名单(二)_第2张图片

nginx访问日志如下

[root@git-server ~]# tail -3 /data/wwwlogs/kjios.com_access.log 
211.144.7.32 - - [05/Jun/2018:23:24:37 +0800] "GET /webapi/124?imsi=46&tel=15800 HTTP/1.1" 403 564 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
211.144.7.32 - - [05/Jun/2018:23:26:38 +0800] "GET /webapi/124?imsi=46&tel=15800 HTTP/1.1" 200 50 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
211.144.7.32 - - [05/Jun/2018:23:26:40 +0800] "GET /webapi/124?imsi=46&tel=15800 HTTP/1.1" 200 50 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"

到此处演示完毕,欢迎网友们一块讨论学习lua+ngnx+redis。