12.10 Nginx訪問日誌
12.11 Nginx日誌切割
12.12 靜態文件不記錄日誌和過時時間javascript
$remote_addr | 客戶端IP(公網IP) |
---|---|
$http_x_forwarded_for | 代理服務器的IP |
$time_local | 服務器本地時間 |
$host | 訪問主機名(域名) |
$request_uri | 訪問的url地址 |
$status | 狀態碼 |
$http_referer | referer |
$http_user_agent | user_agent |
[root@yong-01 vhost]# vim /usr/local/nginx/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"';
$remote_addr | 客戶端IP(公網IP) |
---|---|
$http_x_forwarded_for | 代理服務器的IP |
$time_local | 服務器本地時間 |
$host | 訪問主機名(域名) |
$request_uri | 訪問的url地址 |
$status | 狀態碼 |
$http_referer | referer(跳轉頁) |
$http_user_agent | user_agent(標識) |
[root@yong-01 vhost]# vim test.com.conf 在第一個括號中添加access_log /tmp/test.com.log combined_realip;便可 server { listen 80; server_name test.com test1.com test2.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/test.com.log combined_realip; }
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test1.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Sat, 09 Jun 2018 01:03:43 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/ [root@yong-01 vhost]# curl -x127.0.0.1:80 test2.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Sat, 09 Jun 2018 01:03:58 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/
[root@yong-01 vhost]# cat /tmp/test.com.log 127.0.0.1 - [09/Jun/2018:09:03:43 +0800] test1.com "/" 301 "-" "curl/7.29.0" 127.0.0.1 - [09/Jun/2018:09:03:58 +0800] test2.com "/" 301 "-" "curl/7.29.0"
#! /bin/bash ## 假設nginx的日誌存放路徑爲/data/logs/ d=`date -d "-1 day" +%Y%m%d` logdir="/data/logs" 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@yong-01 vhost]# vim /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`
date -d 「-1 day」 +%Y%m%d
// 生成昨天的日期,格式爲年月日cat $nginx_pid
,而這個命令目的和nginx -s reload 是同樣的ls *.log
//開始語句循環,看錯有哪些 log後綴的文件cat $nginx_pid
// 從新加載,生成一個新的「nginx_pid=」/usr/local/nginx/logs/nginx.pid」[root@yong-01 vhost]# ls aaa.com.conf test.com.conf [root@yong-01 vhost]# for f in `ls` ; do ls -l $f ;done -rw-r--r-- 1 root root 142 6月 7 21:47 aaa.com.conf -rw-r--r-- 1 root root 293 6月 9 09:01 test.com.conf
[root@yong-01 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20180608 + 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-20180608 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1226
[root@yong-01 vhost]# ls /tmp/ mysql.sock pear php-fcgi.sock systemd-private-4f8e9ff25515441f842387e8592cb371-chronyd.service-rUvlFo systemd-private-4f8e9ff25515441f842387e8592cb371-vgauthd.service-Pjk7Gv systemd-private-4f8e9ff25515441f842387e8592cb371-vmtoolsd.service-Fbw1Ir systemd-private-fb16b9da48d04706bda2fb1e850ee20d-chronyd.service-PfzjDV systemd-private-fb16b9da48d04706bda2fb1e850ee20d-vgauthd.service-7voK16 systemd-private-fb16b9da48d04706bda2fb1e850ee20d-vmtoolsd.service-TQ5rlv test.com.log test.com.log-20180608
[root@yong-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
[root@yong-01 vhost]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
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) 」關閉記錄日誌 }
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# cd /data/wwwroot/test.com/ [root@yong-01 test.com]# ls admin admin.php index.html [root@yong-01 test.com]# vim 1.gif [root@yong-01 test.com]# vim 3.js
[root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif fasdfasf [root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/3.js fadsfasgjsd [root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/index.html test.com
[root@yong-01 test.com]# cat /tmp/test.com.log 127.0.0.1 - [09/Jun/2018:09:45:28 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@yong-01 test.com]# curl -x127.0.0.1:80 test.com/3.js -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Sat, 09 Jun 2018 01:46:33 GMT Content-Type: application/x-javascript Content-Length: 12 Last-Modified: Sat, 09 Jun 2018 01:44:11 GMT Connection: keep-alive ETag: "5b1b30eb-c" Expires: Sat, 09 Jun 2018 13:46:33 GMT Cache-Control: max-age=43200 Accept-Ranges: bytes