用日志查“访问来源”通常分为 Web 访问日志(如 Nginx/Apache) 和 系统/应用日志 两类,下面以最常见场景说明。
默认路径:
/var/log/nginx/access.log
常见日志格式示例:
192.168.1.10 - - [10/Oct/2024:12:00:01 +0800] "GET /index.html HTTP/1.1" 200 1024 "https://google.com" "Mozilla/5.0"
各字段含义:
192.168.1.10 → 客户端 IP(访问来源)https://google.com → Referer(从哪跳转来的)Mozilla/5.0... → User-Agent(浏览器/爬虫)awk '{print $1}' access.log | sort | uniq -c | sort -nr | head
awk -F\" '{print $4}' access.log | sort | uniq -c | sort -nr
grep -i "bot" access.log
路径:
/var/log/httpd/access_log
查看来源 IP:
cut -d' ' -f1 access_log | sort | uniq -c | sort -nr
如果前面有 CDN / 负载均衡 / Nginx 反代,真实 IP 在:
X-Forwarded-ForX-Real-IPNginx 日志需包含:
log_format main '$http_x_forwarded_for - $remote_addr ...';
查看:
awk '{print $1}' access.log | sort | uniq -c
查谁登录过服务器:
last
或:
grep "Accepted" /var/log/secure
查看登录 IP:
grep "Accepted password" /var/log/secure | awk '{print $11}'
如 Java / Python:
request.getRemoteAddr()ip、source、clientIp| 目的 | 查什么 |
|---|---|
| 谁访问最多 | 客户端 IP |
| 从哪跳转 | Referer |
| 是否爬虫 | User-Agent |
| 是否被攻击 | 异常 IP + 高频请求 |
| 真实用户地区 | IP + GeoIP |
如果你能告诉我:
我可以直接给你精确命令。