centos

CentOS PHP日志中异常请求如何识别

小樊
39
2025-08-16 19:32:29
栏目: 编程语言

在 CentOS 系统中,PHP 日志通常位于以下几个路径:

  1. Apache: /var/log/httpd/error_log
  2. Nginx + PHP-FPM: /var/log/nginx/error.log

要识别异常请求,你可以使用以下方法:

  1. 使用 grep 命令搜索特定的关键字,例如 404(未找到)或 500(内部服务器错误):

    grep ' 404 ' /var/log/httpd/error_log
    grep ' 500 ' /var/log/httpd/error_log
    

    或者对于 Nginx + PHP-FPM:

    grep ' 404 ' /var/log/nginx/error.log
    grep ' 500 ' /var/log/nginx/error.log
    
  2. 使用 awk 或 cut 命令提取请求的 URL:

    grep ' 404 ' /var/log/httpd/error_log | awk '{print $7}' | cut -d'"' -f2
    grep ' 500 ' /var/log/httpd/error_log | awk '{print $7}' | cut -d'"' -f2
    

    或者对于 Nginx + PHP-FPM:

    grep ' 404 ' /var/log/nginx/error.log | awk '{print $7}' | cut -d'"' -f2
    grep ' 500 ' /var/log/nginx/error.log | awk '{print $7}' | cut -d'"' -f2
    
  3. 使用 sort 和 uniq 命令找出重复出现的异常请求:

    grep ' 404 ' /var/log/httpd/error_log | awk '{print $7}' | cut -d'"' -f2 | sort | uniq -c | sort -nr
    grep ' 500 ' /var/log/httpd/error_log | awk '{print $7}' | cut -d'"' -f2 | sort | uniq -c | sort -nr
    

    或者对于 Nginx + PHP-FPM:

    grep ' 404 ' /var/log/nginx/error.log | awk '{print $7}' | cut -d'"' -f2 | sort | uniq -c | sort -nr
    grep ' 500 ' /var/log/nginx/error.log | awk '{print $7}' | cut -d'"' -f2 | sort | uniq -c | sort -nr
    

这些命令将帮助你找到异常请求及其出现次数。你可以根据这些信息分析问题并采取相应的措施。

0
看了该问题的人还看了