1. 1.独立IP
  2. awk '{print $1}' host.access.log | sort -r |uniq -c | wc -l
  3. 2.统计PV
  4. awk '{print $6}' host.access.log | wc -l
  5. 3.查询访问最频繁的URL
  6. awk '{print $7}' host.access.log|sort | uniq -c |sort -n -k 1 -r|more
  7. 4.查询访问最频繁的IP
  8. awk '{print $1}' host.access.log|sort | uniq -c |sort -n -k 1 -r|more
  9. 5. UV统计:
  10. awk '{print $6}' host.access.log | sort -r |uniq -c |wc -l

conf

  1. location = /favicon.ico {
  2. log_not_found off;
  3. access_log off;
  4. }

IP黑名单

配置共享内存

lua_shared_dict ip_blacklist 10m;#添加到http配置中

通过lua+redis实现IP过滤,文件可放在nginx/lua/ipblacklist.lua

local redis_host="127.0.0.1"
local redis_port=6379
local redis_pass="DL.2021redis"
local redis_key="ip_blacklist"
local cache_ttl=60

local ip=ngx.var.remote_addr
local ip_blacklist=ngx.shared.ip_blacklist
local last_update_time=ip_blacklist:get("last_update_time");
ngx.update_time();

if last_update_time == nil or last_update_time <(ngx.time()-cache_ttl) then
    local redis=require "resty.redis";
    local red=redis.new();
    red:set_timeouts(3000, 2000, 2000)

    local ok,err = red:connect(redis_host, redis_port)
    if not ok then
        ngx.log(ngx.DEBUG,"failed to connect redis:" .. err);
    else
        local res, err = red:auth(redis_pass)
        if not res then
            ngx.log(ngx.DEBUG,"failed to authenticate connect redis:" .. err);
        end
        local new_ip_blacklist,err=red:smembers(redis_key);
        if err then
            ngx.log(ngx.DEBUG,"Redis read erron while retrieving ip blacklist:".. err);
        else
            ip_blacklist:flush_all();
            for index,banned_ip in ipairs(new_ip_blacklist) do
                ip_blacklist:set(banned_ip,true);
            end
        ip_blacklist:set("last_update_time",ngx.time());
        end
    end
end
if ip_blacklist:get(ip) then
    ngx.log(ngx.DEBUG,"Banned IP detected and refused access:" ..ip);
    return ngx.exit(ngx.HTTP_FORBIDDEN);
end

然后在location中配置

access_by_lua_file lua/ipblacklist.lua;