12.10 Nginx訪問日誌php
12.11 Nginx日誌切割css
12.12 靜態文件不記錄日誌和過時時間html
12.10 Nginx訪問日誌:nginx
~1.日誌格式shell
vim /usr/local/nginx/conf/nginx.conf //搜索log_formatvim
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
以上紅色部分(combined_realip)爲日誌格式的名字,能夠隨意更改。好比改爲「axin」,在這定義成什麼名字,在後面引用他的時候就寫成什麼bash
在nginx中,命令是以分號(;)做爲結束的。以上有分行,可是做爲一串配置的服務器
$remote_addr 客戶端IP(公網IP)。也就是遠程的客戶地址,不是咱們192.168這個,是咱們出口的IP。打開百度,搜索IP便可查詢curl
$http_x_forwarded_for 代理服務器的IP工具
$time_local 服務器本地時間
$host 訪問主機名(域名)好比咱們作測試,test.com.conf/admin/index.htmi, test.com.conf就是host
$request_uri 訪問的url地址。好比咱們作測試,test.com.conf/admin/index.htmi, admin/index.html就是url
$status 狀態碼
$http_referer referer 從哪一個頁面連接過來的
$http_user_agent user_agent 用戶的一些信息
~1.1(實例:)
除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件中增長
access_log /tmp/test.com.log axin;
這裏的combined_realip就是在nginx.conf中定義的日誌格式名字
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log
實例:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
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 axin;在主配置文件裏,咱們剛纔定義的訪問日誌的名字就叫axin,這裏就要設定爲axin
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown log format "axin" in /usr/local/nginx/conf/vhost/test.com.conf:10
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
nginx: [emerg] unknown log format "axin" in /usr/local/nginx/conf/vhost/test.com.conf:10
[root@localhost~]# curl -x127.0.0.1:80 test2.com/admin/index.html/hgjkjkgjkhj -I 測試登陸一下
HTTP/1.1 301 Moved Permanently Server: nginx/1.8.0 Date: Tue, 23 Jul 2019 07:34:56 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/admin/index.html/hgjkjkgjkhj
[root@localhost ~]# cat /tmp/test.com.log cat一下
127.0.0.1 - - [23/Jul/2019:15:53:00 +0800] "HEAD HTTP://test2.com/admin/index.html/hgjkjkgjkhj HTTP/1.1" 301 0 "-" "curl/7.29.0"
12.11 Nginx日誌切割: Nginx不像Apache那樣自帶日誌切割的工具,因此要藉助於系統的日誌切割工具,或者本身寫日誌切割的腳本。一下是日誌切割的腳本:
~~1. 自定義shell 腳本 ~1\. vim /usr/local/sbin/nginx\_log\_rotate.sh 寫入以下內容(之後得shell腳本要放在/usr/local/sbin下面)
~2.#! /bin/bash \## 假設nginx的日誌存放路徑爲/tmp/logs/ d=\`date -d "-1 day" +%Y%m%d\` 命令行會敲出昨天的日期。目的是生成昨天的日期 logdir="/tmp/logs" 日誌路徑 nginx_pid="/usr/local/nginx/logs/nginx.pid" 找pid是由於要執行最下面kill的命令。由於從新修改log的路徑,他實際上還在寫以前自帶的日誌,要把他kil掉。這個pid的路徑要找對了,否則最下面的kill執行不成功 cd $logdir 先進入到logdir目錄下 for log in \`ls *.log\` 看一下目錄下有哪些log do mv $log $log-$d 給全部的log更名字。就是切割,好比在0點0分的時候執行,改革名字加個後綴,$d表明上面的變量d,也就是後綴名是昨天的日期 done /bin/kill -HUP \`cat $nginx_pid\` 最後從新加載,生成新的
~3.sh -x /usr/local/sbin/nginx\_log\_rotate sh即執行腳本,-x顯示執行的過程
~4.find /tmp/ -type f -mtime +30 -name \*log-\* | xargs rm 能夠作這項操做,刪除30天之前的日誌 ~~2. 任務計劃 0 0 * * * /bin/bash /usr/local/sbin/nginx\_log\_rotate.sh 還要作一個任務計劃,天天0點0分執行這個腳本
實例: ~~1.切割日誌: [root@localhost ~\]# vim /usr/local/sbin/nginx\_log\_rotate.sh #! /bin/bash d=`date -d "-1 day" +%Y%m%d` logdir="/tmp/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@localhost logs\]# sh -x /usr/local/sbin/nginx\_log\_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20190722 + logdir=/tmp/logs + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/logs /usr/local/sbin/nginx_log_rotate.sh: 第 5 行:cd: /tmp/logs: 沒有那個文件或目錄 ++ ls '*.log' ls: 沒法訪問*.log: 沒有那個文件或目錄 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 3775 ~~2.任務計劃: \[root@localhost logs\]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx\_log\_rotate.sh
12.12 靜態文件不記錄日誌和過時時間:
配置以下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 匹配什麼樣的請求(也是正則)
{
expires 7d; 過時時間
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h; .js|css小文件的過時時間
access_log off;
}
實例:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
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; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; }
[root@localhost 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@localhost test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.jjpg -I 先訪問一個沒定義的.jjpg文件
HTTP/1.1 404 Not Found Server: nginx/1.8.0 Date: Tue, 23 Jul 2019 09:04:11 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.jpg -I 訪問1.jpg
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.gif -I 訪問2.gif
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - - [23/Jul/2019:15:53:00 +0800] "HEAD HTTP://test2.com/admin/index.html/hgjkjkgjkhj HTTP/1.1" 301 0 "-" "curl/7.29.0" 127.0.0.1 - - [23/Jul/2019:17:04:11 +0800] "HEAD HTTP://test.com/1.jjpg HTTP/1.1" 404 0 "-" "curl/7.29.0"