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

[经验分享] 12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期

[复制链接]

尚未签到

发表于 2018-11-11 14:13:57 | 显示全部楼层 |阅读模式
12.10 Nginx访问日志
DSC0000.jpg

  [root@martin001 vhost]# vim test.com.conf
  除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
  access_log /tmp/test.com.log martin;
  这里的combined_realip就是在nginx.conf中定义的日志格式名字
  [root@martin001 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@martin001 vhost]# /usr/local/nginx/sbin/nginx -s>  [root@martin001 vhost]#  curl -x127.0.0.1:80 test.com -I
  HTTP/1.1 200 OK
  Server: nginx/1.12.1
  Date: Wed, 14 Mar 2018 15:39:14 GMT
  Content-Type: text/html
  Content-Length: 9
  Last-Modified: Tue, 13 Mar 2018 16:20:55 GMT
  Connection: keep-alive
  ETag: "5aa7fa67-9"
  Accept-Ranges: bytes
  [root@martin001 vhost]# cat /tmp/test.com.log
  127.0.0.1 - [14/Mar/2018:23:34:33 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"
  127.0.0.1 - [14/Mar/2018:23:39:14 +0800] test.com "/" 200 "-" "curl/7.29.0"
12.11 Nginx日志切割
DSC0001.jpg

  [root@martin001 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
  #! /bin/bash
  d=date -d "-1 day" +%Y%m%d
  logdir="/tmp/"
  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
  [root@martin001 vhost]# ls /usr/local/nginx/logs/nginx.pid
  /usr/local/nginx/logs/nginx.pid
  [root@martin001 vhost]# ls /tmp/
  mysql.sock
  pear
  php-fcgi.sock
  systemd-private-d3d5de9693ed4f8ebbf3d1f91826a8cd-chronyd.service-zjb45v
  systemd-private-d3d5de9693ed4f8ebbf3d1f91826a8cd-vgauthd.service-evgZ7p
  systemd-private-d3d5de9693ed4f8ebbf3d1f91826a8cd-vmtoolsd.service-twe9Yk
  test.com.log
  test.com.log-20180313
  [root@martin001 vhost]# crontab -e
  no crontab for root - using an empty one
  任务计划 crontab -e
  0 0  * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
12.12 静态文件不记录日志和过期时间
DSC0002.jpg

  [root@martin001 vhost]# cat test.com.conf
  server
  {
  listen 80;
  server_name test.com test1.com test2.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 martin;
  location ~ ..(gif|jpg|jpeg|png|bmp|swf)$
  {
  expires      7d;
  access_log off;
  }
  location ~ .*.(js|css)$
  {
  expires      12h;
  access_log off;
  }
  }

  [root@martin001 vhost]# /usr/local/nginx/sbin/nginx -s>  [root@martin001 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@martin001 vhost]# cd /data/wwwroot/test.com/
  [root@martin001 test.com]# ls
  index.html
  [root@martin001 test.com]# vim 1.gif
  [root@martin001 test.com]# vim 2.js
  [root@martin001 test.com]# curl -x127.0.0.1:80 test.com/1.gif
  dfgsdfgsdfgsdfg
  [root@martin001 test.com]# curl -x127.0.0.1:80 test.com/2.js
  dfasdfasdfasdf
  [root@martin001 test.com]# curl -x127.0.0.1:80 test.com/index.html
  test.com
  [root@martin001 test.com]# cat /tmp/test.com.log
  127.0.0.1 - [15/Mar/2018:00:19:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  [root@martin001 test.com]# curl -x127.0.0.1:80 test.com/index.html
  test.com
  [root@martin001 test.com]# cat /tmp/test.com.log
  127.0.0.1 - [15/Mar/2018:00:19:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - [15/Mar/2018:00:20:51 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  [root@martin001 test.com]# curl -x127.0.0.1:80 test.com/2.jsghfgfh
  
  404 Not Found
  
  404 Not Found
  nginx/1.12.1
  
  
  [root@martin001 test.com]# curl -x127.0.0.1:80 test.com/2.js -I
  HTTP/1.1 200 OK
  Server: nginx/1.12.1
  Date: Wed, 14 Mar 2018 16:21:27 GMT
  Content-Type: application/javascript
  Content-Length: 15
  Last-Modified: Wed, 14 Mar 2018 16:18:11 GMT
  Connection: keep-alive
  ETag: "5aa94b43-f"
  Expires: Thu, 15 Mar 2018 04:21:27 GMT
  Cache-Control: max-age=43200
  Accept-Ranges: bytes
  [root@martin001 test.com]# cat /tmp/test.com.log
  127.0.0.1 - [15/Mar/2018:00:19:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - [15/Mar/2018:00:20:51 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
  127.0.0.1 - [15/Mar/2018:00:21:03 +0800] test.com "/2.jsghfgfh" 404 "-" "curl/7.29.0"


运维网声明 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-633768-1-1.html 上篇帖子: nginx alias-peter的未来 下篇帖子: nginx错误汇总
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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