×
新网 > 虚机资讯 > 正文

LNMP架构之访问日志、日志切割、静态文件不记录及过期时间设置

  • 作者:未知
  • 来源:
  • 2018-05-08 14:31:56

本文索引: Nginx访问日志 Nginx日志切割 静态文件不记录日志和过期时间 Nginx访问日志 修改nginx配置文件 [root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf # 搜索:/log_format # 在nginx中以;作为一行的结尾,所以下列代码时一个配置 # 格式:“log_format 日志格式名

本文索引:

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

002UASMrzy7605pjKJv15&690.jpg


Nginx访问日志 修改nginx配置文件

[root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf # 搜索:/log_format # 在nginx中以;作为一行的结尾,所以下列代码时一个配置 # 格式:“log_format 日志格式名 格式;” log_format test \'$remote_addr $http_x_forwarded_for [$time_local]\' \' $host "$request_uri" $status\' \' "$http_referer" "$http_user_agent"\';

格式内使用的变量说明如下:

变量名说明 $remote_addr客户端ip(公网ip) $http_x_forwarded_for代理服务器的ip $time_local服务器本地时间 $host访问主机名(域名) $request_uri访问的url地址 $status状态码 $http_refererreferer $http_user_agentuser_agent 在虚拟主机内定义日志路径

# 在server块内插入 access_log /tmp/test.com.log test; # 格式为access_log 日志存放路径 日志格式名(主配置文件内定义的) 重启服务

[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload 验证效果

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index.php [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index1.php # 成功记录 [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:19:05:22 +0800] test.com "/" 401 "-" "curl/7.29.0" 127.0.0.1 - [03/Jan/2018:19:05:34 +0800] test2.com "/index.php" 301 "-" "curl/7.29.0" 127.0.0.1 - [03/Jan/2018:19:05:39 +0800] test2.com "/index1.php" 301 "-" "curl/7.29.0" Nginx日志切割

nginx没有apache内的rotatelog日志切割命令,我们可以通过自定义shell脚本来实现日志切割的功能。

创建自定义脚本

[root@localhost vhost]# vim /usr/local/sbin/nginx_log_rotate.sh [root@localhost vhost]# cat /usr/local/sbin/nginx_log_rotate.sh #!/bin/bash # date +%Y%m%d 显示的是今天的日期 # 加上 -d "-1 day" 显示的是昨天的日期 d=`date -d "-1 day" +%Y%m%d` # 定义日志存放的路径,虚拟主机配置文件内定义 logdir="/tmp" # pid文件 nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir # 在日志存放路径下循环更改日志文件名 for log in `ls *.log` do mv $log $log-$d done # 在不关闭进程前提下重启,等价于nginx -s reload /bin/kill -HUP `cat $nginx_pid` 脚本测试

# sh -x 可以显示脚本执行的过程 [root@localhost vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d \'-1 day\' +%Y%m%d + d=20180102 + 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-20180102 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1299 # 查看是否实现功能 [root@localhost vhost]# ls /tmp/*.log* /tmp/test.com.log /tmp/test.com.log-20180102 配合crontab命令周期性执行

[root@localhost vhost]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh


静态文件不记录日志和过期时间 修改虚拟主机配置文件

[root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf # ~ 匹配后续的正则表示 # 使用转义.,匹配.jpg等文件 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { # expires设置过期时间 expires 7d; # 关闭日志记录 access_log off; } location ~ .*.(css|js)$ { expires 12h; access_log off; } 测试 验证静态文件不记录日志

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif jhasdifhoai [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js abdsuofghan # gif/js文件日志未记录访问日志 [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [03/Jan/2018:19:36:25 +0800] test.com "/index.php" 401 "-" "curl/7.29.0" 验证过期时间 测试gif的过期时间

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:36:52 GMT Content-Type: image/gif Content-Length: 12 Last-Modified: Wed, 03 Jan 2018 11:35:29 GMT Connection: keep-alive ETag: "5a4cc001-c" Expires: Wed, 10 Jan 2018 11:36:52 GMT # 信息内显示max-age时间 Cache-Control: max-age=604800 Accept-Ranges: bytes 测试js文件的过期时间

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:36:58 GMT Content-Type: application/javascript Content-Length: 12 Last-Modified: Wed, 03 Jan 2018 11:35:44 GMT Connection: keep-alive ETag: "5a4cc010-c" Expires: Wed, 03 Jan 2018 23:36:58 GMT # 信息内显示max-age时间 Cache-Control: max-age=43200 Accept-Ranges: bytes 测试PHP文件,没有max-age信息

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 03 Jan 2018 11:46:58 GMT Content-Type: application/octet-stream Content-Length: 19 Last-Modified: Wed, 03 Jan 2018 11:36:44 GMT Connection: keep-alive ETag: "5a4e1572-13" Accept-Ranges: bytes


  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading