nginx访问日志配置+日志切割+不记录静态文件日志+设置静态文件过期时间

nginx访问日志

查看nginx.conf文件

  1. [root@KevinfeideMacBook ~]# vim /usr/local/nginx/conf/nginx.conf

中间有一行是定义log的格式

  1. log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  2. ' $host "$request_uri" $status'
  3. ' "$http_referer" "$http_user_agent"';
其中’’包围起来的不算一行,只是为了美观才分行显示,分行显示就必须加上’’号;
含义:

combined_realip:定义日志格式别名,默认为combined_realip,这里我会改为test001;
$remote_addr:客户端ip也就是出口公网ip;
$http_x_forwarded_for:代理服务器ip;
$time_local:服务器本地时间;
$host:访问主机名(域名);
$request_uri:访问的url地址;
$status:状态码,比如404;
$http_referer:referer;
$http_user_agent:也就是访问的浏览器类型,比如傲游7;

编辑虚拟web配置

  1. [root@KevinfeideMacBook ~]# vim /usr/local/nginx/conf/vhost/test.com.conf

增加access_log /tmp/test.com.log combined_realip;
其中combined_realip是定义名称,记得与nginx.conf匹配;

nginx检错与重新加载
  1. [root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -t
  2. [root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -s reload
访问与测试
  1. [root@KevinfeideMacBook ~]# curl -x127.0.0.1:80 test.com/1.html -I

访问成功200

查看日志
  1. [root@KevinfeideMacBook ~]# cat /tmp/test.com.log
  2. 127.0.0.1 - [15/Mar/2018:03:13:28 +0800] test.com "/1.html" 200 "-" "curl/7.29.0"
日志切割

创建shell脚本

  1. [root@KevinfeideMacBook ~]# vim /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
假设nginx的日志存放路径为/tmp/
  1. d=`date -d "-1 day" +%Y%m%d`
  2. logdir="/tmp/"
  3. nginx_pid="/usr/local/nginx/logs/nginx.pid"
  4. cd $logdir
  5. for log in `ls *.log`
  6. do
  7. mv $log $log-$d
  8. done
  9. /bin/kill -HUP `cat $nginx_pid`
  10. 执行脚本(可不执行)
  11. sh -x /usr/local/sbin/nginx_log_rotate.sh
  12. [root@KevinfeideMacBook ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
  13. ++ date -d '-1 day' +%Y%m%d
  14. + d=20180314
  15. + logdir=/tmp/
  16. + nginx_pid=/usr/local/nginx/logs/nginx.pid
  17. + cd /tmp/
  18. ++ ls php_errors.log test.com.log
  19. + for log in '`ls *.log`'
  20. + mv php_errors.log php_errors.log-20180314
  21. + for log in '`ls *.log`'
  22. + mv test.com.log test.com.log-20180314
  23. ++ cat /usr/local/nginx/logs/nginx.pid
  24. + /bin/kill -HUP 1574
任务计划执行脚本
  1. [root@KevinfeideMacBook ~]# crontab -e
写入脚本

0 0 * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
意思:每天凌晨0点执行脚本/usr/local/sbin/nginx_log_rotate.sh

如何脚本删除log

静态文件不记录日志和过期时间和编辑web虚拟文件

  1. [root@KevinfeideMacBook ~]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
  2. [root@KevinfeideMacBook ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
增加代码
  1. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  2. {
  3. expires 7d;
  4. access_log off;
  5. }
  6. location ~ .*\.(js|css)$
  7. {
  8. expires 12h;
  9. access_log off;
  10. }
所有代码预览
  1. server
  2. {
  3. listen 80;
  4. server_name test.comtest1.comtest2.com;
  5. index index.html index.htm index.php;
  6. root /data/wwwroot/test.com;
  7. if ($host != 'test.com' ) {
  8. rewrite ^/(.*)$ http://test.com/$1 permanent;
  9. }
  10. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  11. {
  12. expires 7d;
  13. access_log off;
  14. }
  15. location ~ .*\.(js|css)$
  16. {
  17. expires 12h;
  18. access_log off;
  19. }
  20. access_log /tmp/test.com.log test;
  21. }

检错与重新加载

  1. [root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -t
  2. [root@KevinfeideMacBook ~]# /usr/local/nginx/sbin/nginx -s reload
点赞 ( 0 )

0 条评论

发表评论

人生在世,错别字在所难免,无需纠正。

插入图片
s
返回顶部