十二週三次課javascript
12.10Nginx訪問日誌php
12.11Nginx日誌切割css
12.12靜態文件不記錄日誌和過時時間html
12.10Nginx訪問日誌java
•日誌格式mysql
• vim /usr/local/nginx/conf/nginx.conf //搜索log_formatnginx
$remote_addrsql |
客戶端IP(公網IP)shell |
$http_x_forwarded_forvim |
代理服務器的IP |
$time_local |
服務器本地時間 |
$host |
訪問主機名(域名) |
$request_uri |
訪問的url地址 |
$status |
狀態碼 |
$http_referer |
referer |
$http_user_agent |
user_agent |
除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件中增長
access_log /tmp/1.log combined_realip;
這裏的combined_realip就是在nginx.conf中定義的日誌格式名字
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log
1.打開主配置文件/usr/local/nginx/conf/nginx.conf
[root@tianqi-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(標識) |
2.除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件去定義access_log /tmp/1.log combined_realip;來定義訪問日誌路徑.
[root@tianqi-01 vhost]# vim test.com.conf
在第一個括號中添加access_log /tmp/1.log combined_realip;便可
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/test.com.log combined_realip;
}
保存退出
3.而後檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-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@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]#
4.測試
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test4.com/admin.index.html/adafsbs -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:17:23 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test3.com/admin.index.html/adafsbs -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:17:53 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin.index.html/adafsbs
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test2.com/admin.index.html/adafsbs -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:18:07 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin.index.html/adafsbs
5.查看日誌/tmp/test.com.log
[root@tianqi-01 vhost]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:20:17:53 +0800] test3.com "/admin.index.html/adafsbs" 301 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:20:18:07 +0800] test2.com "/admin.index.html/adafsbs" 301 "-" "curl/7.29.0"
[root@tianqi-01 vhost]#
12.11Nginx日誌切割
Nginx日誌切割目錄概要
• 自定義shell 腳本
• vim /usr/local/sbin/nginx_log_rotate.sh//寫入以下內容
#! /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`
• 任務計劃
• 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
1.這裏寫一個日誌切割腳本
2.首先建立一個shell腳本vim /usr/local/sbin/nginx_log_rotate.sh
[root@tianqi-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`
[root@tianqi-01 vhost]# ls
aaa.com.conf test.com.conf
[root@tianqi-01 vhost]# for f in `ls`; do ls -l $f ; done
-rw-r--r-- 1 root root 142 Mar 12 19:38 aaa.com.conf
-rw-r--r-- 1 root root 290 Mar 13 20:15 test.com.conf
[root@tianqi-01 vhost]#
3.執行shell腳本,並加-x
[root@tianqi-01 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180312
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls php_errors.log test.com.log
+ for log in '`ls *.log`'
+ mv php_errors.log php_errors.log-20180312
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180312
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 803
[root@tianqi-01 vhost]#
4.查看日誌切割文件,天天都生成一個日誌,在天天切割後,過段時間還要按期清理
[root@tianqi-01 vhost]# ls /tmp/
mysql.sock
pear
php_errors.log-20180312
php-fcgi.sock
systemd-private-0e2b4ec78d5546abb6c87fc75918c612-vgauthd.service-Tmhc6I
systemd-private-0e2b4ec78d5546abb6c87fc75918c612-vmtoolsd.service-bKnQdM
systemd-private-10939be3906d4885a694c9ccaa969513-vgauthd.service-up8a1e
systemd-private-10939be3906d4885a694c9ccaa969513-vmtoolsd.service-qrMrpr
systemd-private-1e99790229834f839348180fcbb3aa93-vgauthd.service-v7jGAu
systemd-private-1e99790229834f839348180fcbb3aa93-vmtoolsd.service-pRTqgc
systemd-private-2d7e7760b6cd416895880c21a3911a53-vgauthd.service-S1F6Rz
systemd-private-2d7e7760b6cd416895880c21a3911a53-vmtoolsd.service-muJvMP
systemd-private-491ebf486ca940d885ed9348c17675f5-vgauthd.service-3sBthy
systemd-private-491ebf486ca940d885ed9348c17675f5-vmtoolsd.service-sJ80kS
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vgauthd.service-DBvBHm
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vmtoolsd.service-AjXoy6
systemd-private-7ad9977beb634b3280df7fd64e4b16d6-vgauthd.service-qkAtId
systemd-private-7ad9977beb634b3280df7fd64e4b16d6-vmtoolsd.service-z35BeI
systemd-private-7d038e320b614122b872e50458348353-vgauthd.service-Ka4suT
systemd-private-7d038e320b614122b872e50458348353-vmtoolsd.service-radGF1
systemd-private-889a29715b47432482f67cbf18e2c4ac-vgauthd.service-hGaIS9
systemd-private-889a29715b47432482f67cbf18e2c4ac-vmtoolsd.service-gDfN0p
systemd-private-aa279ed589d542bcbc4f2903687ce469-vgauthd.service-xDYbZY
systemd-private-aa279ed589d542bcbc4f2903687ce469-vmtoolsd.service-tTo26l
systemd-private-f4d35a41f34c4234a6934c03885429ba-vgauthd.service-owt3oB
systemd-private-f4d35a41f34c4234a6934c03885429ba-vmtoolsd.service-eJFX1G
test.com.log
test.com.log-20180312
[root@tianqi-01 vhost]#
5.刪除30天之前的日誌文件
[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
rm: missing operand
Try 'rm --help' for more information.
[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30
[root@tianqi-01 vhost]#
//不會刪除任何的文件,由於根本沒與符合要求的文件
[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f
/tmp/php_errors.log-20180312
/tmp/test.com.log-20180312
[root@tianqi-01 vhost]# ls /usr/local/sbin/
iptables.sh nginx_log_rotate.sh
[root@tianqi-01 vhost]# 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`
[root@tianqi-01 vhost]#
6.完腳本後,還要加一個任務計劃crontab -e——>這裏由於是測試,腳本就不加入到任務計劃中了
[root@tianqi-01 vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
//直接退出不保存
1.日誌切割的定義
date -d 「-1 day」 +%Y%m%d
」不明白意思[root@tianqi-01 vhost]# date -d "-1 day" +%Y%m%d
20180312
[root@tianqi-01 vhost]# date
Tue Mar 13 21:16:08 CST 2018
[root@tianqi-01 vhost]#
2.指定PID路徑的意義
cat $nginx_pid
」這個命令也將沒有辦法繼續執行cat $nginx_pid
」 z這條命令的意思就是從新加載一次nginx服務cat $nginx_pid
」這條命令的目的是由於切割日誌之後 「mv $log $log-$d 」 會將日誌移動位置,若是不使用這條命令從新加載一次nginx服務、從新生成一第二天志文件,那麼將會致使服務出錯cat $nginx_pid
」能準確的執行,須要肯定nginx的PID所在[root@tianqi-01 vhost]# ls /usr/local/nginx/logs/
access.log error.log nginx_error.log nginx.pid
[root@tianqi-01 vhost]#
3.循環語句理解
12.12靜態文件不記錄日誌和過時時間
靜態文件不記錄日誌和過時時間目錄概要
• 配置以下
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) 」關閉記錄日誌
}
1.打開虛擬主機配置文件
[root@tianqi-01 vhost]# vim /usr/local/nginx/conf/vhost/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;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/test.com.log combined_realip;
}
保存退出
2.檢查配置文件語法錯誤,並從新加載配置文件
[root@tianqi-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@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]#
3.測試,先來模擬一個圖片
[root@tianqi-01 vhost]# cd /data/wwwroot/test.com/
[root@tianqi-01 test.com]# ls
admin index.html
[root@tianqi-01 test.com]# vim 1.gif //在裏面隨便寫點東西
asdbasdgb
保存退出
[root@tianqi-01 test.com]# vim 2.js //在裏面隨便寫點東西
sasgashv
保存退出
4.接下來作訪問測試
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
asdbasdgb
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
sasgashv
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
4.查看日誌,會看到只有一條日誌
[root@tianqi-01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
sasgashv
[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@tianqi-01 test.com]#
//這說明訪問js的時候不會記錄日誌
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.jsagasg
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:36:51 +0800] test.com "/2.jsagasg" 404 "-" "curl/7.29.0"
[root@tianqi-01 test.com]#
5.測試過時時間,加上-I參數
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 13:38:09 GMT
Content-Type: application/javascript
Content-Length: 9
Last-Modified: Tue, 13 Mar 2018 13:31:38 GMT
Connection: keep-alive
ETag: "5aa7d2ba-9"
Expires: Wed, 14 Mar 2018 01:38:09 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
[root@tianqi-01 test.com]#
6.若是去掉expires,則不會顯示max-age過時時間
[root@tianqi-01 test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf
# expires 12h;
//註釋掉上面這一句話
7.查看配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 test.com]# /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@tianqi-01 test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 test.com]#
8.從新進行curl訪問,則max-age=43200不會出現
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 13:41:49 GMT
Content-Type: application/javascript
Content-Length: 9
Last-Modified: Tue, 13 Mar 2018 13:31:38 GMT
Connection: keep-alive
ETag: "5aa7d2ba-9"
Accept-Ranges: bytes
[root@tianqi-01 test.com]#
友情連接:阿銘Linux