1.独立IP
awk '{print $1}' host.access.log | sort -r |uniq -c | wc -l
2.统计PV
awk '{print $6}' host.access.log | wc -l
3.查询访问最频繁的URL
awk '{print $7}' host.access.log|sort | uniq -c |sort -n -k 1 -r|more
4.查询访问最频繁的IP
awk '{print $1}' host.access.log|sort | uniq -c |sort -n -k 1 -r|more
5. UV统计:
awk '{print $6}' host.access.log | sort -r |uniq -c |wc -l
conf
location = /favicon.ico {
log_not_found off;
access_log off;
}
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;