Nginx訪問日誌
- 日誌格式
vim /usr/local/nginx/conf/nginx.conf //搜索log_format
下圖中就是Nginx的日誌格式:
combined_realip是日誌格式名,我們可以自己自定義,這裏我們改爲xie_log
字段 | 含義 |
---|---|
$remote_addr | 客戶端IP(公網IP) |
$http_x_forwarded_for | 代理服務器的IP |
$time_local | 服務器本地時間 |
$host | 訪問主機名(域名) |
$request_uri | 訪問的url地址 |
$status | 狀態碼 |
$http_referer | referer |
$http_user_agent | user_agent |
- 定義test.com的日誌:
用curl訪問test.com後查看日誌:
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` //跟Nginx的-s重新加載配置文件一樣
執行一下這個腳本,sh執行,-x是顯示執行的過程:
sh -x /usr/local/sbin/nginx_log_rotate.sh
crontab -e 添加任務計劃:
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh //每天凌晨0點執行這個腳本
靜態文件不記錄日誌和過期時間
- vim /usr/local/nginx/conf/vhost/test.com.conf
配置如下:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //匹配靜態文件
{
expires 7d; //過期時間
access_log off;
}
location ~ .*\.(js|css)$ //匹配js,css文件
{
expires 12h;
access_log off;
}
保存退出後記得-t,-s測試加載配置文件
curl測試:
以gif,js等等這些文件結尾的靜態文件都不會記錄在日誌中