웹 서버 로그에는 사람의 요청과 봇의 요청이 섞여 있습니다. SEO 크롤러는 유익하지만, 스크래핑 봇, 스캐너, DDoS 봇은 차단해야 합니다. 로그 분석으로 봇 트래픽을 구분하고 대응하는 방법을 알아봅니다.
`` Googlebot/2.1 (+http://www.google.com/bot.html) Bingbot/2.0 (+http://www.bing.com/bingbot.htm) facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php) `
이들은 robots.txt를 준수하며 적정 속도로 크롤링합니다.
` python-requests/2.28.0 curl/7.68.0 Scrapy/2.5.0 Nikto/2.1.6 (Vulnerability Scanner) sqlmap/1.5 (Security Testing) `
`bash grep -iE "(bot|crawler|spider|scraper|curl|python-requests|scrapy)" /var/log/nginx/access.log | wc -l `
`bash grep -iE "(Mozilla|Chrome|Safari|Firefox|Edge)" /var/log/nginx/access.log \ | grep -viE "(bot|crawler|spider)" | wc -l `
`bash awk -F'"' '{print $6}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20 `
사람: 페이지 로드 → 읽기 → 클릭 (수 초~수십 초) 봇: 요청 → 즉시 다음 요청 (수 밀리초)
IP별 평균 요청 간격:
`bash awk '{print $1, $4}' /var/log/nginx/access.log | sort | uniq -c `
` GET /page?id=1 GET /page?id=2 GET /page?id=3 ... (숫자만 증가) `
`bash awk -F'"' '$4 == "-" {print $1}' /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn `
`bash awk '{print $1, $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20 `
출력 예시:
` 1234 192.0.2.100 /api/products 1122 198.51.100.7 /sitemap.xml `
`nginx if ($http_user_agent ~* (scrapy|curl|python-requests|sqlmap|nikto)) { return 403; } `
`nginx map $http_user_agent $is_bot { default 1; ~*(googlebot|bingbot|yandex|duckduckbot|facebookexternalhit) 0; ~(Mozilla.Chrome|Mozilla.Firefox|Mozilla.Safari) 0; }
server { if ($is_bot = 1) { return 403; } } `
User-Agent만으로는 부족합니다. 헤더를 변조한 봇은 정상 브라우저처럼 위장합니다.
진짜 브라우저만 JavaScript를 실행할 수 있습니다.
`html `
Nginx에서 쿠키 확인:
`nginx if ($http_cookie !~* "js_enabled=1") { return 403; } `
의심스러운 트래픽에만 CAPTCHA 표시:
`html `
`bash TOTAL=$(wc -l < /var/log/nginx/access.log) BOTS=$(grep -icE "(bot|crawler|spider|curl|python)" /var/log/nginx/access.log) echo "Bots: $BOTS / Total: $TOTAL = $((100 * BOTS / TOTAL))%" `
`bash grep -iE "(bot|crawler|spider)" /var/log/nginx/access.log \ | awk '{print $4}' | cut -d: -f2 | sort | uniq -c ``
| 특징 | 유익한 봇 | 악의적 봇 |
|---|---|---|
| ------ | ---------- | ---------- |
| User-Agent | 공식 이름 명시 | 변조 또는 일반 브라우저 |
| robots.txt | 준수 | 무시 |
| 요청 간격 | 1~5초 | 수백ms |
| 접근 경로 | 링크 구조 따름 | 순차 증가 (id=1,2,3...) |
| Referer | 있음 | 대부분 없음 |
봇 트래픽은 User-Agent, 요청 간격, 경로 패턴, Referer 유무로 구분할 수 있습니다. SEO 크롤러는 허용하되, 스크래핑 봇과 스캐너는 Nginx에서 User-Agent 기반으로 차단하거나 JavaScript Challenge로 걸러내야 합니다.