×
新网 > 域名资讯 > 正文

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

12.10 Nginx访问日志 之前拷贝的nginx.conf下就有关于访问日志的相关 打开配置文件 [root@aminglinux-02 ~]# vim /usr/local/nginx/conf/nginx.conf log_format combined_realip \'$remote_addr $http_x_forwarded_for [$time_local]\' \'

12.10 Nginx访问日志

之前拷贝的nginx.conf下就有关于访问日志的相关

t01dc0f21f1dcb73ef8.png
打开配置文件

[root@aminglinux-02 ~]# vim /usr/local/nginx/conf/nginx.conf log_format combined_realip \'$remote_addr $http_x_forwarded_for [$time_local]\' \' $host "$request_uri" $status\' \' "$http_referer" "$http_user_agent"\';

combined_realip 日志格式的名字,决定了虚拟主机引用日志的类型
nginx配置文件,以 “ ; ” 分号结尾,配置文件一段如果没有 分号结尾,表示这一段还没有结束,就算中间执行了换行。

$remote_addr //客户端IP(公网IP) $http_x_forwarded_for //代理服务器的IP $time_local //服务器本地时间 $host //访问主机名(域名) $request_uri //访问的url地址 $status //状态码 $http_referer //referer (跳转页) $http_user_agent //user_agent(标识)

定义虚拟主机日志
打开虚拟主机配置文件
在server项内任意一行加入下面这个配置

access_log /tmp/test.com.log combined_realip;

配置好以后
检查语法 && 重新加载服务

-t && -s reload

测试是否产生日志

[root@aminglinux-02 ~]# curl -x127.0.0.1:80 test2.com/admin/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Fri, 11 Aug 2017 16:51:26 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/admin/index.html [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test2.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Fri, 11 Aug 2017 16:51:31 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/ [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test1.com -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Fri, 11 Aug 2017 16:51:36 GMT Content-Type: text/html Content-Length: 26 Last-Modified: Thu, 10 Aug 2017 16:27:09 GMT Connection: keep-alive ETag: "598c895d-1a" Accept-Ranges: bytes [root@aminglinux-02 ~]# cat /tmp/test.com.log 127.0.0.1 - [12/Aug/2017:00:51:17 +0800] test3.com "/admin/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 - [12/Aug/2017:00:51:26 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 - [12/Aug/2017:00:51:31 +0800] test2.com "/" 301 "-" "curl/7.29.0" 12.11 Nginx日志切割

nginx没有自带日志切割工具
只能借助系统自带的工具或者使用脚本实现

创建shell脚本

规则:所有的shell的脚本,全部存放在/usr/local/sbin/ 目录下

[root@aminglinux-02 ~]# vim /usr/local/sbin/nginx_logrotate.sh #! /bin/bash ## 假设nginx的日志存放路径为/data/logs/ d=`date -d "-1 day" +%Y%m%d` // 生成昨天的日期,格式为年月日 logdir="tmp/" // 上一节的时候,定义了日志存放在/tmp/目录下 nginx_pid="/usr/local/nginx/logs/nginx.pid" //查找nginx的PID cd $logdir //进入“logdir”目录 for log in `ls *.log` //开始语句循环,看错有哪些 log后缀的文件 do //执行 mv $log $log-$d //将 log改名为《原名字-“`date -d "-1 day" +%Y%m%d` ”这个结尾的文件 》 done //结束 /bin/kill -HUP `cat $nginx_pid` // 重新加载,生成一个新的“ nginx_pid="/usr/local/nginx/logs/nginx.pid" ”

执行脚本

sh -x /usr/local/sbin/nginx_logrotate.sh

-x选项使运行过程可见

[root@aminglinux-02 ~]# sh -x /usr/local/sbin/nginx_logrotate.sh ++ date -d \'-1 day\' +%Y%m%d + d=20170811 + 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-20170811 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1202 知识点: 一、日志时间切割的定义

写shell脚本的时候,如果有命令不明白,可以直接把命令运行一下就知道结果了
假设这个命令“ d=date -d "-1 day" +%Y%m%d ”不明白意思
ctrl+z 把当前操作暂停丢到后台
运行命令看看结果

[root@aminglinux-02 ~]# date -d "-1 day" +%Y%m%d 20170811 [root@aminglinux-02 ~]# date 2017年 08月 12日 星期六 01:04:07 CST

就是时间,而且是昨天的时间,因为目前做的日志切割都是以天为单位,而且,日志需要过了当天23点59分59秒以后到第二天的0点0分01秒才切割

二、指定PID路径的意义 “ nginx_pid="/usr/local/nginx/logs/nginx.pid" ”这条命令的意思,就是指定nginx的PID 的路径所在 如果找不到指定PID的所在,那么下面的“ /bin/kill -HUP cat $nginx_pid ”这个命令也将没有办法继续执行 “ /bin/kill -HUP cat $nginx_pid ” z这条命令的意思就是重新加载一次nginx服务 执行“ /bin/kill -HUP cat $nginx_pid ”这条命令的目的是因为切割日志以后 “mv $log $log-$d ” 会将日志移动位置,如果不使用这条命令重新加载一次nginx服务、重新生成一次日志文件,那么将会导致服务出错 所以,为了保证“ /bin/kill -HUP cat $nginx_pid ”能准确的执行,需要确定nginx的PID所在

[root@aminglinux-02 ~]# ls /usr/local/nginx/logs/ access.log error.log nginx_error.log nginx.pid 三、 循环语句理解

for f in \'ls \' ; do ls -l $f; done

for 循环开始,f 表示文件,in 表示做什么,‘ls’in执行的东西; do 执行 ls -f $f;done 结束

任务计划

脚本写完以后,需要写一个计划,让脚本在规定的时间运行。

crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh

==长时间累积,会生成大量的日志需要进行清理==

fidn /tmp/ -type f -name *.log-* -mtime +30 |xargs rm 12.12 静态文件不记录日志和过期时间

打开虚拟主机配置文件

[root@aminglinux-02 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ //匹配gif|jpg|jpeg|png|bmp|swf 后缀的文件 { expires 7d; //7天后过期 access_log off; //匹配“.*.(gif|jpg|jpeg|png|bmp|swf) ”关闭记录日志 } location ~ .*.(js|css)$ { expires 12h; //12个小时后过期 access_log off; //匹配“.*.(js|css) ”关闭记录日志 }

配置完成后检查语法和重新加载服务

-t && -s reload

测试配置是否成功
为了方便测试线创建2个文件

[root@aminglinux-02 ~]# vim /data/wwwroot/test.com/1.gif [root@aminglinux-02 ~]# vim /data/wwwroot/test.com/2.js

测试是否记录日志

[root@aminglinux-02 ~]# curl -x127.0.0.1:80 test.com/2.js 2.js 2.js 2.js 2.js 2.js 2.js 2.js 2.js 2.js 2.js 2.js [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test.com/1.gif 1.gif 1.gif 1.gif 1.gif 1.gif 1.gif 1.gif 1.gif 1.gif 1.gif 1.gif [root@aminglinux-02 ~]# !cat cat /tmp/test.com.log 127.0.0.1 - [12/Aug/2017:01:48:01 +0800] test.com "/" 200 "-" "curl/7.29.0" 127.0.0.1 - [12/Aug/2017:01:50:13 +0800] test.com "/2.jsfsdfe" 404 "-" "curl/7.29.0"

测试结果,并没有记录 测试是否有过期时间

[root@aminglinux-02 ~]# curl -x127.0.0.1:80 test.com/1.gif -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Fri, 11 Aug 2017 17:52:33 GMT Content-Type: image/gif Content-Length: 66 Last-Modified: Fri, 11 Aug 2017 17:51:27 GMT Connection: keep-alive ETag: "598dee9f-42" Expires: Fri, 18 Aug 2017 17:52:33 GMT Cache-Control: max-age=604800 //这就是过期时间 Accept-Ranges: bytes [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test.com/2.js -I HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Fri, 11 Aug 2017 17:52:51 GMT Content-Type: application/javascript Content-Length: 57 Last-Modified: Fri, 11 Aug 2017 17:51:59 GMT Connection: keep-alive ETag: "598deebf-39" Expires: Sat, 12 Aug 2017 05:52:51 GMT Cache-Control: max-age=43200 //这就是过期时间 Accept-Ranges: bytes

  • 相关专题

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

免费咨询获取折扣

Loading