1.先看看日志格式php
#日志的路径 [root@centos7mei ~]# vim /usr/local/ngin/conf/nginx.conf #搜索log_format #注意下面这段 log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
名词解释:css
名称 | 解释 |
---|---|
$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.刚才在主配置文件中定义了日志格式,接下来还须要在虚拟主机配置中定义日志的储存路径,最后面指定日志的格式名字html
[root@centos7mei ~]# cd /usr/local/nginx/conf/vhost/ [root@centos7mei vhost]# ls default.conf test.com.conf #编辑配置文件 [root@centos7mei vhost]# vim test.com.conf #内容以下 [root@centos7mei vhost]# cat test.com.conf server { listen 80; server_name test.com test2.com test3.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/1.log combined_realip; } #检查读写并从新加载服务 [root@centos7mei 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@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload
3.测试nginx
#访问两次网站 [root@centos7mei vhost]# curl -x127.0.0.1:80 test3.com/index.html/SDFAS -I HTTP/1.1 301 Moved Permanently Server: nginx/1.8.0 Date: Fri, 17 Aug 2018 18:24:04 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/index.html/SDFAS [root@centos7mei vhost]# curl -x127.0.0.1:80 test2.com/index.html/SDFAS -I HTTP/1.1 301 Moved Permanently Server: nginx/1.8.0 Date: Fri, 17 Aug 2018 18:24:23 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/index.html/SDFAS #这里的问题是直接抄视频,没看本身写的具体路径 [root@centos7mei vhost]# cat /tmp/test.com.log cat: /tmp/test.com.log: No such file or directory #这下面就能看到访问日志了 [root@centos7mei vhost]# cat /tmp/1.log 127.0.0.1 - [18/Aug/2018:02:24:04 +0800] test3.com "/index.html/SDFAS" 301 "-" "curl/7.29.0" 127.0.0.1 - [18/Aug/2018:02:24:23 +0800] test2.com "/index.html/SDFAS" 301 "-" "curl/7.29.0"
1.添加脚本shell
[root@centos7mei vhost]# vim /usr/local/sbin/nginx_log_rotate.sh #脚本内容 [root@centos7mei vhost]# cat /usr/local/sbin/nginx_log_rotate.sh #! /bin/bash ## 假设nginx的日志存放路径为/data/logs/ 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@centos7mei vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20180817 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls 1.log + for log in '`ls *.log`' + mv 1.log 1.log-20180817 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 16489
2,测试vim
#清理文件 [root@centos7mei vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm #这里报错是由于没有知足条件的文件 rm: missing operand Try 'rm --help' for more information. #查找下 [root@centos7mei vhost]# find /tmp/ -name *.log-* -type f /tmp/1.log-20180817 ## 最后添加计划任务,清理文件总不能咱们本身天天手动清理吧 [root@centos7mei vhost]# crontab -e #内容 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
ps: 不记录日志就是说咱们在访客记录中看不到,访客访问静态文件的信息centos
1.编辑配置文件缓存
#进入虚拟主机配置文件 [root@centos7mei vhost]# vim test.com.conf #内容 [root@centos7mei vhost]# cat test.com.conf server { listen 80; server_name test.com test2.com test3.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前面的括号完 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { #过时时间 expires 7d; #表示 不记录访问日志 access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } access_log /tmp/1.log combined_realip; } #检查读写和从新加载服务 [root@centos7mei 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@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload
2.测试bash
#这里咱们建立两个图片文件,就是图片格式结尾的文件,而后去访问,最后查看访问日志 [root@centos7mei vhost]# cd /data/wwwroot/test.com/ [root@centos7mei test.com]# ls index.html #这里马虎的出现了一个符号错误 [root@centos7mei test.com]# vim 1,gif [root@centos7mei test.com]# vim 2.gs #致使访问出现404 [root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/1.gif <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.8.0</center> </body> </html> #原来是点写成了逗号,根本没那个文件,因此报错了 [root@centos7mei test.com]# ls 1,gif 2.gs index.html [root@centos7mei test.com]# rm -f 1,gif [root@centos7mei test.com]# ls 2.gs index.html [root@centos7mei test.com]# vim 1.gif [root@centos7mei test.com]# ls 1.gif 2.gs index.html #链接下 [root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/1.gif fdsafs: [root@centos7mei test.com]# curl -x127.0.0.1:80 test.com/2.gs dasdfsfdadf [root@centos7mei test.com]# curl -I -x127.0.0.1:80 test.com/1.GIF HTTP/1.1 404 Not Found Server: nginx/1.8.0 Date: Sat, 18 Aug 2018 01:21:14 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive #添加-I选项能看到更加详细的信息 [root@centos7mei test.com]# curl -I -x127.0.0.1:80 test.com/1.gif HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Sat, 18 Aug 2018 01:21:21 GMT Content-Type: image/gif Content-Length: 8 Last-Modified: Sat, 18 Aug 2018 01:04:52 GMT Connection: keep-alive ETag: "5b7770b4-8" Expires: Sat, 25 Aug 2018 01:21:21 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes #这里能看到咱们访问的1.gif(小写的)就没有记录到日志里面了 [root@centos7mei test.com]# cat /tmp/1.log 127.0.0.1 - [18/Aug/2018:03:35:40 +0800] test2.com "/index.html/SDFAS" 301 "-" "curl/7.29.0" 127.0.0.1 - [18/Aug/2018:09:21:14 +0800] test.com "/1.GIF" 404 "-" "curl/7.29.0"