PHP统计nginx访问日志中的搜索引擎抓取404链接页面路径
author:一佰互联 2019-04-29   click:179

我在服务器上有每天切割nginx日志的习惯,所以针对每天各大搜索引擎来访,总能记录一些404页面信息,传统上我只是偶尔分析下日志,但是对于很多日志信息的朋友,人工来筛选可能不是一件容易的事情,这不我个人自己慢慢研究了一点点,针对谷歌、百度、搜搜、360搜索、宜搜、搜狗、必应等搜索引擎的404访问生成为一个txt文本文件,直接上代码test.php。

复制代码 代码如下:
<?php
//访问test.php?s=google
$domain="//www.yx10011.com";
$spiders=array("baidu"=>"Baiduspider","360"=>"360Spider",
"google"=>"Googlebot","soso"=>"Sosospider","sogou"=>
"Sogou web spider","easou"=>"EasouSpider","bing"=>"bingbot");
 
$path="/home/nginx/logs/".date("Y/m/").(date("d")-1)."/access_www.txt";
 
$s=$_GET["s"];
 
if(!array_key_exists($s,$spiders)) die();
$spider=$spiders[$s];
 
$file=$s."_".date("ym").(date("d")-1).".txt";
if(!file_exists($file)){
    $in=file_get_contents($path);
    $pattern="/GET (.*) HTTP/1.1" 404.*".$spider."/";
    preg_match_all ( $pattern , $in , $matches );
    $out="";
    foreach($matches[1] as $k=>$v){
        $out.=$domain.$v." ";
    }
    file_put_contents($file,$out);
}
 
$url=$domain."/silian/".$file;
echo $url;

好就这样了。没有什么高深的技术,只有动手写的过程。