1、nginx访问日记:
日记格式:在主配置文件nginx.conf里搜索log_format;
[root@localhost_001 conf]# vim nginx.conflog_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(标识) |
(2):还需要在虚拟主机vhost目录下的虚拟主机配置文件来定义访问日记所存在的路径;
[root@localhost_001 vhost]# vim test.com.conf [root@localhost_001 vhost]# cat test.com.conf server{ listen 80; server_name www.test.com bbs.test.com test1.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'www.test.com' ) { rewrite ^/(.*)$ http://www.test.com/$1 permanent; } access_log /tmp/test.com.log combined_realip; #定义访问日记的路径;}
注释:如果不写日志格式,那就会走默认的日志格式;
(3):检测配置文件是否存在错误,并重新加载配置文件;
[root@localhost_001 conf]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost_001 conf]# /usr/local/nginx/sbin/nginx -s reload
(4):测试,curl命令访问;
[root@localhost_001 conf]# curl -x127.0.0.1:80 www.test.com -IHTTP/1.1 200 OKServer: nginx/1.4.7Date: Tue, 16 Oct 2018 08:54:49 GMTContent-Type: text/htmlContent-Length: 15Last-Modified: Tue, 16 Oct 2018 06:36:04 GMTConnection: keep-aliveETag: "5bc586d4-f"Accept-Ranges: bytes[root@localhost_001 conf]# curl -x127.0.0.1:80 bbs.test.com -IHTTP/1.1 301 Moved PermanentlyServer: nginx/1.4.7Date: Tue, 16 Oct 2018 08:55:09 GMTContent-Type: text/htmlContent-Length: 184Connection: keep-aliveLocation: http://www.test.com/
(6):查看访问日记;
[root@localhost_001 vhost]# tail /tmp/test.com.log 127.0.0.1 - [16/Oct/2018:16:54:49 +0800] www.test.com "/" 200 "-" "curl/7.29.0"127.0.0.1 - [16/Oct/2018:16:55:09 +0800] bbs.test.com "/" 301 "-" "curl/7.29.0"192.168.149.135 - [16/Oct/2018:16:56:16 +0800] www.test.com "/" 304 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
2、nginx日记切割;
注释:nginx没有自带日志切割工具,只能借助系统的日志切割的工具或者自己写切割的脚本实现;
[root@localhost_001 vhost]# vim /usr/local/sbin/nginx_log_rotate.shcat /usr/local/sbin/nginx_log_rotate.sh#! /bin/bashd=`date -d "-1 day" +%Y%m%d` logdir="/tmp/"nginx_pid="/usr/local/nginx/logs/nginx.pid"cd $logdirfor log in `ls *.log`do mv $log $log-$ddone/bin/kill -HUP `cat $nginx_pid`
注释:d=`date -d "-1 day" +%Y%m%d` : 生成昨天的日记,格式为年月日;
logdir="/tmp" : 定义一个目录;
nginx_pid="/usr/local/nginx/logs/nginx.pid" : 查找nginx的pid,目的是为了执行/bin/kill -HUP `cat $nginx_pid`
/bin/kiall -HUP `cat $nginx_pid` :重新加载,生成一个新的/nginx_pid='/usr/local/nginx/logs/nginx.pid'
(2):执行shell脚本,并加x查看执行过程;
[root@localhost_001 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d+ d=20181015+ 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-20181015++ cat /usr/local/nginx/logs/nginx.pid+ /bin/kill -HUP 6959
(3):查看切割后的日记文件;
[root@localhost_001 vhost]# ls -la /tmp/test*-rw-r--r-- 1 root root 0 10月 16 17:28 /tmp/test.com.log-rw-r--r-- 1 root root 349 10月 16 16:56 /tmp/test.com.log-20181015
注释:还需要写入到crontab里才可以;
[root@localhost_001 vhost]# crontab -l0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
(4):有时候日记切割后,也需要定时删除的; 定时删除30天前的日记;
[root@localhost_001 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm -fr
3、静态文件不记录日记和过期时间;
编辑虚拟主机配置文件: /usr/local/nginx/conf/vhost/test.com.conf
[root@localhost_001 vhost]# vim test.com.conf server{ listen 80; server_name www.test.com bbs.test.com test1.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'www.test.com' ) { rewrite ^/(.*)$ http://www.test.com/$1 permanent; } access_log /tmp/test.com.log combined_realip;#下面是配置不记录静态文件不记录日记和过期时间的配置; 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) ”关闭记录日志 }}
(2):检测并重新加载配置文件;
[root@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(3):测试:用curl命令来测试;
首先使用xhell的rz命令上传一张 .jpg的图片和.js的文件;如下:
[root@localhost_001 test.com]# ls11.js admin.php index.html kaola.jpg
(4):接下来用curl命令来做访问测试;
[root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com/kaola.jpg -IHTTP/1.1 200 OKServer: nginx/1.4.7Content-Type: image/jpegLast-Modified: Tue, 14 Jul 2009 05:32:31 GMT[root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com/11.js -IHTTP/1.1 200 OKServer: nginx/1.4.7Content-Type: application/x-javascriptContent-Length: 46122[root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.comtest.com fast
(5):查看日记,只看到一条访问日记;
[root@localhost_001 test.com]# tail /tmp/test.com.log127.0.0.1 - [16/Oct/2018:17:57:13 +0800] www.test.com "/" 200 "-" "curl/7.29.0"
(6):测试过期时间,加上 -I 选项;
[root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com/kaola.jpg -IHTTP/1.1 200 OKServer: nginx/1.4.7Date: Tue, 16 Oct 2018 09:58:42 GMTContent-Type: image/jpegContent-Length: 780831Last-Modified: Tue, 14 Jul 2009 05:32:31 GMTConnection: keep-aliveETag: "4a5c186f-bea1f"Expires: Tue, 23 Oct 2018 09:58:42 GMTCache-Control: max-age=604800Accept-Ranges: bytes
注释:max-age=604800 过期时间;
注释:如果去掉配置文件中的expires,则不会显示过期时间;