设为首页 收藏本站
查看: 1135|回复: 0

[经验分享] Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间介绍

[复制链接]

尚未签到

发表于 2018-11-10 09:30:50 | 显示全部楼层 |阅读模式
  Nginx访问日志
  1. 进入配置文件
  [root@gary-tao src]# vim /usr/local/nginx/conf/nginx.conf  //搜索log_format
  参考更改配置成如下:
  log_format aming '$remote_addr $http_x_forwarded_for [$time_local]'
  如图:
DSC0000.png

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

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

  3.测试语法及重新加载配置
  [root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s>  4.使用curl测试
  [root@gary-tao src]# 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
  [root@gary-tao src]# 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
  [root@gary-tao src]# 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
  [root@gary-tao src]# cat /tmp/test.com.log
  127.0.0.1 - [04/Jan/2018:17:19:09 +0800] test.com "/" 200 "-" "curl/7.29.0"
  127.0.0.1 - [04/Jan/2018:17:19:54 +0800] test2.com "/admin" 301 "-" "curl/7.29.0"
  127.0.0.1 - [04/Jan/2018:17:20:04 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"
  Nginx日志切割
  1.自定义一个脚本
  [root@gary-tao src]# 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是显示执行的过程
  [root@gary-tao src]# 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
  [root@gary-tao src]# 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.任务计划
  [root@gary-tao src]# crontab -e //添加任务计划
  增加如下内容:
  0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
  静态文件不记录日志和过期时间
  1.修改虚拟主机配置文件
  [root@gary-tao src]# 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;
  }
  如图:
DSC0003.png

  2.测试语法及重新加载配置
  [root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s>  3.使用curl测试
  [root@gary-tao src]# cd /data/wwwroot/test.com
  [root@gary-tao test.com]# ls
  admin  index.html
  [root@gary-tao test.com]# vim 1.gif
  [root@gary-tao test.com]# echo "dgagadgadgs" > /data/wwwroot/test.com/2.js
  [root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/1.gif
  dggagadggagdag
  [root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.js
  dgagadgadgs
  [root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/index.html
  “test.com”
  [root@gary-tao test.com]# cat /tmp/test.com.log
  127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  [root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/index.html
  “test.com”
  [root@gary-tao test.com]# cat /tmp/test.com.log
  127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  [root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.js
  dgagadgadgs
  [root@gary-tao test.com]# cat /tmp/test.com.log
  127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  [root@gary-tao test.com]# curl -x127.0.0.1:80 test.com/2.jsagdaga
  
  404 Not Found
  
  404 Not Found
  nginx/1.12.1
  
  
  [root@gary-tao test.com]# cat /tmp/test.com.log
  127.0.0.1 - [04/Jan/2018:18:53:20 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - [04/Jan/2018:18:53:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - [04/Jan/2018:18:55:22 +0800] test.com "/2.jsagdaga" 404 "-" "curl/7.29.0"
  [root@gary-tao test.com]# 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633112-1-1.html 上篇帖子: nginx相关优化 下篇帖子: nginx内嵌变量
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表