温馨提示×

怎样用日志查访问来源

小樊
38
2026-09-12 11:30:35
栏目: 编程语言

用日志查“访问来源”通常分为 Web 访问日志(如 Nginx/Apache)系统/应用日志 两类,下面以最常见场景说明。


一、Web 日志查访问来源(最常用)

1️⃣ Nginx 访问日志

默认路径:

/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.comReferer(从哪跳转来的)
  • Mozilla/5.0...User-Agent(浏览器/爬虫)

查看访问来源 IP

awk '{print $1}' access.log | sort | uniq -c | sort -nr | head

查看 Referer 来源

awk -F\" '{print $4}' access.log | sort | uniq -c | sort -nr

查看是否是爬虫

grep -i "bot" access.log

二、Apache 日志

路径:

/var/log/httpd/access_log

查看来源 IP:

cut -d' ' -f1 access_log | sort | uniq -c | sort -nr

三、查真实公网来源(穿透代理)

如果前面有 CDN / 负载均衡 / Nginx 反代,真实 IP 在:

  • X-Forwarded-For
  • X-Real-IP

Nginx 日志需包含:

log_format main '$http_x_forwarded_for - $remote_addr ...';

查看:

awk '{print $1}' access.log | sort | uniq -c

四、系统登录来源(SSH)

查谁登录过服务器:

last

或:

grep "Accepted" /var/log/secure

查看登录 IP:

grep "Accepted password" /var/log/secure | awk '{print $11}'

五、应用日志(示例)

如 Java / Python:

  • request.getRemoteAddr()
  • 查日志中的 ipsourceclientIp

六、常见分析目的对照表

目的 查什么
谁访问最多 客户端 IP
从哪跳转 Referer
是否爬虫 User-Agent
是否被攻击 异常 IP + 高频请求
真实用户地区 IP + GeoIP

如果你能告诉我:

  • 用的什么系统(Linux / Windows)
  • 什么服务(Nginx / Apache / Tomcat / 自建程序)
  • 想查“IP / 渠道 / 爬虫 / 攻击”

我可以直接给你精确命令

0