Nginx配置:訪問日誌,日誌切割,靜態文件不記錄日誌和過期時間
阿新 • • 發佈:2018-03-14
Nginx配置 訪問日誌 一、訪問日誌
為日誌格式名字,可以改為其他的,後面可以調用它。
1、查看Nginx日誌格式
[root@zhulinux-02 ~]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
‘ $host "$request_uri" $status‘
‘ "$http_referer" "$http_user_agent"‘;
也可以使用vim
打開nginx配置文件,搜索關鍵字log_format
。
變量說明:combined_realip
2、虛擬主機指定訪問日誌路徑
[root@zhulinux-02 ~]# cd /usr/local/nginx/conf/vhost/ [root@zhulinux-02 vhost]# ls linuxtest.conf moved.conf zlinux.conf [root@zhulinux-02 vhost]# vim linuxtest.conf server { listen 80; server_name linuxtest.com; index index.html index.htm index.php; root /data/wwwroot/linuxtest; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } access_log /tmp/linuxtest.log combined_realip; } #使用access_log指定日誌存儲路徑和使用的日誌格式名字
3、測試
[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 linuxtest.com/ -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.2 Date: Wed, 14 Mar 2018 07:08:32 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/ -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 14 Mar 2018 07:08:53 GMT Content-Type: text/html Content-Length: 31 Last-Modified: Tue, 13 Mar 2018 07:33:35 GMT Connection: keep-alive ETag: "5aa77ecf-1f" Accept-Ranges: bytes [root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/ -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 14 Mar 2018 07:09:00 GMT Content-Type: text/html Content-Length: 31 Last-Modified: Tue, 13 Mar 2018 07:33:35 GMT Connection: keep-alive ETag: "5aa77ecf-1f" Accept-Ranges: bytes [root@zhulinux-02 vhost]# ls /tmp/linuxtest.log /tmp/linuxtest.log [root@zhulinux-02 vhost]# cat /tmp/linuxtest.log 127.0.0.1 - [14/Mar/2018:15:08:32 +0800] linuxtest.com "/" 401 "-" "curl/7.29.0" 127.0.0.1 - [14/Mar/2018:15:08:53 +0800] linuxtest.com "/" 200 "-" "curl/7.29.0" 127.0.0.1 - [14/Mar/2018:15:09:00 +0800] linuxtest.com "/" 200 "-" "curl/7.29.0"
二、訪問日誌切割
Nginx不自帶日誌切割工具,要想切割Nginx日誌需要借助系統的切割工具或者自定義腳本。這裏提供一個Nginx日誌切割腳本供參考。
1、編寫日誌切割腳本
[root@zhulinux-02 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
# date +%Y%m%d 顯示的是今天的日期
# 加上 -d "-1 day" 顯示的是昨天的日期
d=`date -d "-1 day" +%Y%m%d`
#定義日誌存放的路徑,假設存放在/tmp/下
logdir="/tmp"
# pid文件
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
# 在日誌存放路徑下循環更改日誌文件名
for log in `ls *.log`
do
mv $log $log-$d
done
# 在不關閉進程前提下重啟,等價於nginx -s reload
/bin/kill -HUP `cat $nginx_pid`
2、驗證
[root@zhulinux-02 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh //-x選項是調試選項
++ date -d ‘-1 day‘ +%Y%m%d
+ d=20180313
+ logdir=/tmp
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp
++ ls linuxtest.log
+ for log in ‘`ls *.log`‘
+ mv linuxtest.log linuxtest.log-20180313
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 14119
[root@zhulinux-02 vhost]# ls /tmp/ //生成了帶日期的日誌
linuxtest.log mysql.sock php-fcgi.sock
linuxtest.log-20180313 pear systemd-private-ee3d7331f2804e42bc9bfe8f2b90a0e8-vmtoolsd.service-3B2v7u
3、增加任務計劃
[root@zhulinux-02 vhost]# crontab -e
no crontab for root - using an empty one //介入以下內容
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
三、配置靜態文件不記錄和過期時間
1、修改虛擬主機配置
[root@zhulinux-02 vhost]# vim linuxtest.conf
server
{
listen 80;
server_name linuxtest.com;
index index.html index.htm index.php;
root /data/wwwroot/linuxtest;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
# ~ 匹配後續的正則表示
# 使用\轉義.,匹配.jpg等文件
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
# expires設置過期時間
expires 7d;
# 關閉日誌記錄
access_log off;
}
location ~ .*\.(css|js)$
{
expires 12h;
access_log off;
}
access_log /tmp/linuxtest.log combined_realip;
}
2、測試
[root@zhulinux-02 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@zhulinux-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@zhulinux-02 vhost]# echo "111111" > /data/wwwroot/linuxtest/test.js
[root@zhulinux-02 vhost]# echo "111111" > /data/wwwroot/linuxtest/test.jpg
[root@zhulinux-02 vhost]# echo "111111" > /data/wwwroot/linuxtest/test.ddd //創建一個對比文件,後綴名不在配置列表內
[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:1234566 linuxtest.com/test.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:45:47 GMT
Content-Type: application/javascript
Content-Length: 7
Last-Modified: Wed, 14 Mar 2018 07:44:42 GMT
Connection: keep-alive
ETag: "5aa8d2ea-7"
Expires: Wed, 14 Mar 2018 19:45:47 GMT
Cache-Control: max-age=43200 //最大過期時間
Accept-Ranges: bytes
[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/test.jpg -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:45:55 GMT
Content-Type: image/jpeg
Content-Length: 7
Last-Modified: Wed, 14 Mar 2018 07:45:05 GMT
Connection: keep-alive
ETag: "5aa8d301-7"
Expires: Wed, 21 Mar 2018 07:45:55 GMT
Cache-Control: max-age=604800 //最大過期時間
Accept-Ranges: bytes
[root@zhulinux-02 vhost]# curl -x127.0.0.1:80 -u zlinux:123456linuxtest.com/test.ddd -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 07:46:01 GMT
Content-Type: application/octet-stream
Content-Length: 7
Last-Modified: Wed, 14 Mar 2018 07:45:29 GMT
Connection: keep-alive
ETag: "5aa8d319-7"
Accept-Ranges: bytes
[root@zhulinux-02 vhost]# cat /tmp/linuxtest.log //未記錄指定後綴日誌
127.0.0.1 - [14/Mar/2018:15:42:57 +0800] linuxtest.com "/1.gif" 404 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:15:46:01 +0800] linuxtest.com "/test.ddd" 200 "-" "curl/7.29.0"
Nginx配置:訪問日誌,日誌切割,靜態文件不記錄日誌和過期時間