一、nginx訪問日記:javascript
日記格式:在主配置文件nginx.conf裏搜索log_format;php
[root@localhost_001 conf]# vim 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表示日記格式的名字,能夠隨便定義,這裏定義成什麼名字,後面引用的時候就是什麼名字,決定了虛擬主機引用日記類型;css
註釋:nginx的配置文件每一段是以分號結尾,配置這一段若是沒有分號,則表示這一段尚未結束;html
$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目錄下的虛擬主機配置文件來定義訪問日記所存在的路徑;java
[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; #定義訪問日記的路徑; }
註釋:若是不寫日誌格式,那就會走默認的日誌格式;nginx
(3):檢測配置文件是否存在錯誤,並從新加載配置文件;shell
[root@localhost_001 conf]# /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_001 conf]# /usr/local/nginx/sbin/nginx -s reload
(4):測試,curl命令訪問;vim
[root@localhost_001 conf]# curl -x127.0.0.1:80 www.test.com -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 08:54:49 GMT Content-Type: text/html Content-Length: 15 Last-Modified: Tue, 16 Oct 2018 06:36:04 GMT Connection: keep-alive ETag: "5bc586d4-f" Accept-Ranges: bytes [root@localhost_001 conf]# curl -x127.0.0.1:80 bbs.test.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 08:55:09 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://www.test.com/
(6):查看訪問日記;bash
[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"
二、nginx日記切割;服務器
註釋:nginx沒有自帶日誌切割工具,只能藉助系統的日誌切割的工具或者本身寫切割的腳本實現;
[root@localhost_001 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh cat /usr/local/sbin/nginx_log_rotate.sh #! /bin/bash 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`
註釋: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 -l 0 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
三、靜態文件不記錄日記和過時時間;
編輯虛擬主機配置文件: /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 -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_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(3):測試:用curl命令來測試;
首先使用xhell的rz命令上傳一張 .jpg的圖片和.js的文件;以下:
[root@localhost_001 test.com]# ls 11.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 -I HTTP/1.1 200 OK Server: nginx/1.4.7 Content-Type: image/jpeg Last-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 -I HTTP/1.1 200 OK Server: nginx/1.4.7 Content-Type: application/x-javascript Content-Length: 46122 [root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com test.com fast
(5):查看日記,只看到一條訪問日記;
[root@localhost_001 test.com]# tail /tmp/test.com.log 127.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 -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 09:58:42 GMT Content-Type: image/jpeg Content-Length: 780831 Last-Modified: Tue, 14 Jul 2009 05:32:31 GMT Connection: keep-alive ETag: "4a5c186f-bea1f" Expires: Tue, 23 Oct 2018 09:58:42 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes
註釋:max-age=604800 過時時間;
註釋:若是去掉配置文件中的expires,則不會顯示過時時間;