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"