Nginx访问日志、日志切割、静态文件管理

  • 来源:
  • 更新日期:2018-05-07

摘要:12.10 访问日志 Nginx日志格式: [root@cham002 vhost]# vim ../nginx.conf log_format cham \'$remote_addr $http_x_forwarded_for [$time_local]\' \' $host \"$request_uri\" $status\' \' \"$http_referer\" \"$http_us

12.10 访问日志

Nginx日志格式:

[root@cham002 vhost]# vim ../nginx.conf log_format cham \'$remote_addr $http_x_forwarded_for [$time_local]\' \' $host "$request_uri" $status\' \' "$http_referer" "$http_user_agent"\';

y2vzmvxcck4.jpg

说明:
“combined_ realip”:日志格式名称;\'$remote_ addr $http_ x_ forwarded_ for [$time_ local]\' \' $host "$request_uri" $status\' \' "$http_ referer" "$http_ user_ agent"\' :日志内容。
注释:

名称 含义 $remote_addr 客户端IP(公网IP) $http_x_forwarded_for 代理服务器的IP $time_local 服务器本地时间 $host 访问主机名(域名) $request_uri 访问的URL地址 $status 状态码 $http_referer referer $http_user_agent user_agent 定义虚拟主机日志格式

定义虚拟主机的前提是在Nginx配置文件中设定日志格式,然后才能在虚拟主机中进行调用(格式名称)。

 

[root@cham002 vhost]# vim test.com.conf server { listen 80; server_name test.com test2.com test3.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != \'test.com\' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } access_log /tmp/test.com.log cham; } [root@cham002 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@cham002 vhost]# /usr/local/nginx/sbin/nginx -s reload

注:如果不指定日志格式,系统使用默认日志格式,记录内容较简单。

测试

[root@cham002 vhost]# curl -x127.0.0.1:80 test.com/admin/index.html -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:03:08 GMT Content-Type: text/html Content-Length: 20 Last-Modified: Wed, 03 Jan 2018 08:50:53 GMT Connection: keep-alive ETag: "5a4c996d-14" Accept-Ranges: bytes [root@cham002 vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:03:20 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/admin/index.html [root@cham002 vhost]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:21:03:08 +0800] test.com "/admin/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [03/Jan/2018:21:03:20 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0" 12.11 Nginx日志切割

因为Nginx没有自带的日志切割工具,所以需要借助系统日志切割命令或使用日志切割脚本。

日志切割脚本

为了方便管理,shell脚本统一保存位置:/usr/local/sbin/

[root@cham002 vhost]# vim /usr/local/sbin/nginx_logrotate.sh #! /bin/bash d=`date -d "-1 day" +%Y%m%d` #定义切割时间(切割一天前的日志) logdir="/tmp/" #此处指定要切割的日志路径(该路径来自虚拟主机配置文件) nginx_pid="/usr/local/nginx/logs/nginx.pid" #调用pid的目的是执行命令:/bin/kill -HUP `cat $nginx_pid` #该命令等价于命令:nginx -s reload(重新加载文件),确保与虚拟主机配置文件变更保持同步 #该地址来自nginx配置文件 cd $logdir for log in `ls *.log` do mv $log $log-$d done #此处使用通配进行循环,对所有复合条件的日志文件进行切割 /bin/kill -HUP `cat $nginx_pid` #执行此命令进行重载生成新的日志文件来记录新的日志

执行该脚本:

[root@cham002 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d \'-1 day\' +%Y%m%d + d=20180102 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls php_errors.log test.com.log + for log in \'`ls *.log`\' + mv php_errors.log php_errors.log-20180102 + for log in \'`ls *.log`\' + mv test.com.log test.com.log-20180102 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 39707

说明:-x选项的作用是显示脚本执行过程。

注:该脚本配合任务计划cron使用,定期进行切割和清理。

12.12 静态文件不记录日志&过期时间

核心配置参数:

[root@cham002 vhost]# vim test.com.conf location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ #匹配文件类型 { expires 7d; #过期时间为7天 access_log off; #不记录该类型文件的访问日志 } location ~ .*.(js|css)$ { expires 12h; #过期时间为12小时 access_log off; #不记录该类型文件的访问日志 } access_log /tmp/test.com.log cham; #指定日志位置及格式

 

检测及测试

[root@cham002 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@cham002 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@cham002 vhost]# cd /data/wwwroot/test.com/ [root@cham002 test.com]# ls admin index.html [root@cham002 test.com]# vim 1.gif [root@cham002 test.com]# vim 2.js [root@cham002 test.com]# curl -x127.0.0.1:80 test.com/1.gif 123wsfwfdwfadgdgdsfsdfdsafdsfds [root@cham002 test.com]# curl -x127.0.0.1:80 test.com/2.js sdfsdfdsfsdfdsfdsfdsf123123 [root@cham002 test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [root@cham002 test.com]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:21:35:36 +0800] test.com "/index.html" 200 "-" "curl/7.29. [root@cham002 test.com]# curl -x127.0.0.1:80 -I test.com/2.js HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:37:42 GMT Content-Type: application/javascript Content-Length: 28 Last-Modified: Wed, 03 Jan 2018 13:34:48 GMT Connection: keep-alive ETag: "5a4cdbf8-1c" Expires: Thu, 04 Jan 2018 01:37:42 GMT Cache-Control: max-age=43200 Accept-Ranges: bytes