淑昊柠 发表于 2018-11-10 09:30:50

Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间介绍

  Nginx访问日志
  1. 进入配置文件
  # vim /usr/local/nginx/conf/nginx.conf//搜索log_format
  参考更改配置成如下:
  log_format aming '$remote_addr $http_x_forwarded_for [$time_local]'
  如图:

  日志格式字段含义如下:
  combined_realip为日志格式的名字,后面可以调用它。

  2.到虚拟主机配置文件中指定访问日志的路径
  # vim /usr/local/nginx/conf/vhost/test.com.conf
  增加如下内容:
  access_log /tmp/test.com.log combined_realip;
  //这里的combined_realip就是在nginx.conf中定义的日志格式名字
  如图:

  3.测试语法及重新加载配置
  # /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

  # /usr/local/nginx/sbin/nginx -s>  4.使用curl测试
  # curl -x127.0.0.1:80 test.com -I
  HTTP/1.1 200 OK
  Server: nginx/1.12.1
  Date: Thu, 04 Jan 2018 09:19:09 GMT
  Content-Type: text/html
  Content-Length: 15
  Last-Modified: Wed, 03 Jan 2018 13:03:42 GMT
  Connection: keep-alive
  ETag: "5a4cd4ae-f"
  Accept-Ranges: bytes
  # curl -x127.0.0.1:80 test2.com/admin -I
  HTTP/1.1 301 Moved Permanently
  Server: nginx/1.12.1
  Date: Thu, 04 Jan 2018 09:19:54 GMT
  Content-Type: text/html
  Content-Length: 185
  Connection: keep-alive
  Location: http://test.com/admin
  # curl -x127.0.0.1:80 test2.com/admin/index.html -I
  HTTP/1.1 301 Moved Permanently
  Server: nginx/1.12.1
  Date: Thu, 04 Jan 2018 09:20:04 GMT
  Content-Type: text/html
  Content-Length: 185
  Connection: keep-alive
  Location: http://test.com/admin/index.html
  # cat /tmp/test.com.log
  127.0.0.1 - test.com "/" 200 "-" "curl/7.29.0"
  127.0.0.1 - test2.com "/admin" 301 "-" "curl/7.29.0"
  127.0.0.1 - test2.com "/admin/index.html" 301 "-" "curl/7.29.0"
  Nginx日志切割
  1.自定义一个脚本
  # vim /usr/local/sbin/nginx_log_rotate.sh
  定义如下内容:
  #!/bin/bash
  ## 假设nginx的日志存放路径为/data/logs/
  d=`date -d "-1 day" +%Y%m%d`   //这个日期是昨天的日期,因为日志切割是第二天才执行这个脚本的。
  logdir="/data/logs"
  nginx_pid="/usr/local/nginx/logs/nginx.pid"
  cd $logdir
  for log in `ls *.log`
  do
  mv $log $log-$d
  done
  /bin/kill -HUP `cat $nginx_pid` //跟Nginx的-s重新加载配置文件一样
  2.执行脚本
  sh执行,-x是显示执行的过程
  # sh -x /usr/local/sbin/nginx_log_rotate.sh
  ++ date -d '-1 day' +%Y%m%d
  + d=20180103
  + 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-20180103
  + for log in '`ls *.log`'
  + mv test.com.log test.com.log-20180103
  ++ cat /usr/local/nginx/logs/nginx.pid
  + /bin/kill -HUP 62748
  # ls /tmp/
  mysql.sock                                                                systemd-private-b666888e47f84d62afce0dcb90bdfc91-vmtoolsd.service-09q5L2
  pear                                                                      systemd-private-fdc53ff508e94ecda3c5a90dad98a792-vmtoolsd.service-O5O620
  php_errors.log-20180103                                                   test.com.log
  php-fcgi.sock                                                             test.com.log-20180103
  systemd-private-6fc6799999fe42dd97426bde338fb145-vmtoolsd.service-qi1M6k
  3.任务计划
  # crontab -e //添加任务计划
  增加如下内容:
  0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
  静态文件不记录日志和过期时间
  1.修改虚拟主机配置文件
  # vim /usr/local/nginx/conf/vhost/test.com.conf
  增加如下内容:
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$//匹配脱义静态文件
  {
  expires      7d;   //配置过期时间
  access_log off;
  }
  location ~ .*\.(js|css)$    //匹配js,css文件
  {
  expires      12h;
  access_log off;
  }
  如图:

  2.测试语法及重新加载配置
  # /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

  # /usr/local/nginx/sbin/nginx -s>  3.使用curl测试
  # cd /data/wwwroot/test.com
  # ls
  adminindex.html
  # vim 1.gif
  # echo "dgagadgadgs" > /data/wwwroot/test.com/2.js
  # curl -x127.0.0.1:80 test.com/1.gif
  dggagadggagdag
  # curl -x127.0.0.1:80 test.com/2.js
  dgagadgadgs
  # curl -x127.0.0.1:80 test.com/index.html
  “test.com”
  # cat /tmp/test.com.log
  127.0.0.1 - test.com "/index.html" 200 "-" "curl/7.29.0"
  # curl -x127.0.0.1:80 test.com/index.html
  “test.com”
  # cat /tmp/test.com.log
  127.0.0.1 - test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - test.com "/index.html" 200 "-" "curl/7.29.0"
  # curl -x127.0.0.1:80 test.com/2.js
  dgagadgadgs
  # cat /tmp/test.com.log
  127.0.0.1 - test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - test.com "/index.html" 200 "-" "curl/7.29.0"
  # curl -x127.0.0.1:80 test.com/2.jsagdaga
  
  404 Not Found
  
  404 Not Found
  nginx/1.12.1
  
  
  # cat /tmp/test.com.log
  127.0.0.1 - test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - test.com "/2.jsagdaga" 404 "-" "curl/7.29.0"
  # curl -x127.0.0.1:80 -I test.com/2.js
  HTTP/1.1 200 OK
  Server: nginx/1.12.1
  Date: Thu, 04 Jan 2018 10:56:11 GMT
  Content-Type: application/javascript
  Content-Length: 12
  Last-Modified: Thu, 04 Jan 2018 10:51:59 GMT
  Connection: keep-alive
  ETag: "5a4e074f-c"
  Expires: Thu, 04 Jan 2018 22:56:11 GMT
  Cache-Control: max-age=43200//43200秒表示过期时间12小时,与配置文件里一样。
  Accept-Ranges: bytes

页: [1]
查看完整版本: Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间介绍