博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx访问日志、日志切割、静态文件不记录日志和过期时间
阅读量:6865 次
发布时间:2019-06-26

本文共 4856 字,大约阅读时间需要 16 分钟。

hot3.png

11月27日任务

12.10 Nginx访问日志

12.11 Nginx日志切割

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

 

12.10 Nginx访问日志

a4b09fa2f29288e5898b624e7fff81e2393.jpg

  • 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
  •  access_log /tmp/1.log combined_realip;
  •  这里的combined_realip就是在nginx.conf中定义的日志格式名字
  •  -t && -s reload
  •  curl -x127.0.0.1:80 test.com -I
  •  cat /tmp/1.log

[root@zgxlinux-01 vhost]# vim ../nginx.conf         #修改日志名称

6527647d65c9e22f28f3f9cb10e6a6b94a7.jpg

[root@zgxlinux-01 vhost]# vim test.com.conf 

0172ae26b32141f04a10e9388525192ffb6.jpg

[root@zgxlinux-01 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@zgxlinux-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@zgxlinux-01 vhost]# curl -x127.0.0.1:80 test4.com/admin/index.html/asdkfh -I

HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 03:20:47 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@zgxlinux-01 vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html/asdkfh -I

HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 03:20:54 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/asdkfh

[root@zgxlinux-01 vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html/asdkfh -I

HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Sat, 01 Dec 2018 03:21:08 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/asdkfh

[root@zgxlinux-01 vhost]# cat /tmp/test.com.log 

127.0.0.1 - [01/Dec/2018:11:20:54 +0800] test3.com "/admin/index.html/asdkfh" 301 "-" "curl/7.29.0"
127.0.0.1 - [01/Dec/2018:11:21:08 +0800] test2.com "/admin/index.html/asdkfh" 301 "-" "curl/7.29.0"
 

 

12.11 、Nginx日志切割

  • 自定义shell 脚本
  •  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`

  •  任务计划
  •  0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

 

#操作过程

[root@zgxlinux-01 vhost]# vim /usr/local/sbin/nginx_logrotate.sh

86728cd41346138f90810738039d033d33c.jpg

[root@zgxlinux-01 vhost]# sh -x  /usr/local/sbin/nginx_logrotate.sh       #执行这个脚本,-x表示查看执行过程

++ date -d '-1 day' +%Y%m%d
+ d=20181130
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20181130
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 4421
[root@zgxlinux-01 vhost]# ls /tmp/
mysql.sock
pear
php-fcgi.sock
systemd-private-bbc1c842e4c14aefa0295c2d1c669add-chronyd.service-yevkTQ
test.com.log
test.com.log-20181130
[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm   #长时间以后需要清理日志,xargs进行删除操作。
rm: 缺少操作数
Try 'rm --help' for more information.

[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f 

/tmp/test.com.log-20181130
[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f |xargs rm -rf
[root@zgxlinux-01 vhost]# find /tmp/ -name *.log-* -type f 
[root@zgxlinux-01 vhost]# ls /tmp/
mysql.sock
pear
php-fcgi.sock
systemd-private-bbc1c842e4c14aefa0295c2d1c669add-chronyd.service-yevkTQ
test.com.log
[root@zgxlinux-01 vhost]# ls /usr/local/sbin/
iptables.sh  nginx_logrotate.sh
[root@zgxlinux-01 vhost]# cat !$/nginx_logrotate.sh
cat /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"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`

 

 

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

  • 配置如下

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }
 

 

#操作过程

[root@zgxlinux-01 vhost]# vim test.com.conf 

56217fa2e103d89c17d5c20cc8840666f2e.jpg

[root@zgxlinux-01 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@zgxlinux-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@zgxlinux-01 vhost]# cd /data/wwwroot/test.com/
[root@zgxlinux-01 test.com]# ls
admin  index.html
[root@zgxlinux-01 test.com]# vim 1.gif
[root@zgxlinux-01 test.com]# vim 2.js

[root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif

kdhfkadshfkashkdh
[root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
akdksfalsdjflajdfns,shvawekhfkl
[root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@zgxlinux-01 test.com]# cat /tmp/test.com.log 
127.0.0.1 - [01/Dec/2018:12:07:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@zgxlinux-01 test.com]# curl -x127.0.0.1:80 test.com/2.jsdsfdss
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

转载于:https://my.oschina.net/u/3959708/blog/2965084

你可能感兴趣的文章
《Android应用开发入门经典(第3版)》——导读
查看>>
xmemcached发布1.3.6
查看>>
《Nmap渗透测试指南》—第6章6.4节IP欺骗
查看>>
Samba 系列(九):将 CentOS 7 桌面系统加入到 Samba4 AD 域环境中
查看>>
《C Primer Plus(第6版)中文版》一第1章 初识C语言1.1 C语言的起源
查看>>
《C语言及程序设计》实践参考——当年第几天
查看>>
前端使用fis3开启本地服务器,并实现热加载功能
查看>>
看BAT技术面试官如何挑选Java程序员
查看>>
AI强势来袭,锁上手机就真的安全了吗?
查看>>
Spring 中的 context
查看>>
重构代码(应如写诗)
查看>>
Vue混入mixins
查看>>
前阿里 P9 级员工称离婚是模拟测试,已回滚复婚!
查看>>
衡阳a货翡翠,南平a货翡翠
查看>>
大姨太入场,EtcGame全线升级为Coingame,开启ETH投注倒计时……
查看>>
阿里云HBase推出全新X-Pack服务 定义HBase云服务新标准
查看>>
通过Auto Layout深入了解SizeClasses的好处和使用
查看>>
Spring scope解惑
查看>>
BCH与BCE共享比特币之名
查看>>
js脚本 处理js注入
查看>>