💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
一)错误日志 1、介绍 功能:配置记录nginx启动及运行过程中所产生的错误,是调试Nginx服务的重要手段,属于核心模块 2、命令 error_log 语法: Syntax: error_log file [level]; Default: error_log logs/error.log error; 案例: server { listen 80; server_name www.a.com a.com; error_log /vhosts/web1/logs/error_log; if ( $http_host != "www.a.com" ){ rewrite ^/(.*)$ http://www.a.com/$1 permanent; } location / { root "/vhosts/web1"; index index.html index.htm; } location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; } error_page 404 =302 /404.html; } 说明:可以为每个location配置不同的错误路径 二)访问日志 1)log_format log_format name [escape=default|json] string ...; 案例: log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; $remote_addr 客户端地址 $http_x_forwarded_for 设置web节点记录客户端地址的配置 $remote_user 客户端用户名称 $request 请求的host $status 状态 $body_bytes_sents 报文主体大小 $http_referer 上一个跳转页面 $http_user_agent 客户端浏览器 $request_time 请求处理时间 2)access_log 语法: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; 案例: server { listen 80; server_name www.a.com a.com; error_log /vhosts/web1/logs/error_log; access_log /vhosts/web1/logs/access_log; if ( $http_host != "www.a.com" ){ rewrite ^/(.*)$ http://www.a.com/$1 permanent; } location / { root "/vhosts/web1"; index index.html index.htm; } [root@static logs]# /usr/local/nginx/sbin/nginx -s reload [root@static logs]# ll total 4 -rw-r--r--. 1 root root 0 Dec 15 15:31 access_log -rw-r--r--. 1 root root 194 Dec 15 14:50 error_log 查看 [root@static logs]# tail access_log 10.2.18.231 - - [15/Dec/2017:15:34:46 +0800] "GET / HTTP/1.1" 200 18 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" 三)open_log_file_cache 语法: Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; Default: open_log_file_cache off; 对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭 功能: 定义一个缓存,存储常用日志的文件描述符,这些日志的名称包含变量 max: 设置缓存描述符的最大数量 inactive: 设置在此期间没有访问的缓存描述符 min_uses: 设置在inactive时间内,日志文件最少使用多少次后,该日志文件描述符记录缓存中,默认为1次 valid:设置检查频率 默认60s 案例: open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m; 四)Nginx日志切割配置 默认情况Nginx会把所有访问日志生成到一个指定的访问日志文件access.log,但如果这样,时间久了就会导致日志文件非常庞大,不利于日志的分析和处理,因此有必要对nginx日志,按天和小时进行切割,分成不同的文件保存 #!/bin/bash Date=`date +%F` InstallDir="/usr/local/nginx" NginxLogDir="/vhosts/web1/logs" LogName="access_a" ##### [ -d $NginxLogDir ] && cd $NginxLogDir || exit 1 [ -f ${LogName}.log ]|| exit 1 #Cut logs by day /bin/mv ${LogName}.log ${LogName}_${Date}.log $InstallDir/sbin/nginx -s reload #Delete logs 7 days ago find . -ctime +7 -name "*.log"|xargs rm -f 00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh >/dev/null 2>&1 1)分析截止到目前,当天访问量最高的ip排行 [root@test logs]# awk '{print $1}' 91als.access_2017-12-15.log | sort |uniq -c|sort -nr|head -10 42636 "106.120.160.75" 40419 "106.120.161.65" 368 "101.226.162.89" 363 "125.88.222.250" 335 "123.125.80.235" 278 "113.89.70.105" 276 "144.76.8.134" 232 "113.89.68.65" 194 "106.120.168.109" 2)分析从早上9点至中午12点总的访问量 sed -n "/2017:09:00/,/2016:12:00/"p access_log|wc -l 3) 找出当前日志中502或者404错误的页面并统计