編輯Nginx配置文件:
[root@LampLinux ~]# vim /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"';
將內容更改爲:
log_format linan '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
編輯虛擬主機配置文件:
[root@LampLinux ~]# vim /usr/local/nginx/conf/vhosts/test.conf
在 “root /data/www” 下面寫入:
access_log /tmp/access.log linan; (紅字用戶名對應上面更改後的用戶名)
檢查並重加載:
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -t
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -s reload
刷新網頁,我們查看日誌:
[root@LampLinux ~]# cat /tmp/access.log
發現記錄了很多內容,其中圖片信息沒有必要去記錄。
配置不記錄指定文件日誌:
編輯虛擬主機配置文件:
[root@LampLinux ~]# vim /usr/local/nginx/conf/vhosts/test.conf
在"用戶認證"配置下面寫入:
location ~ .*\.(gif|jpg|png|jpeg|bmp|swf)$
{
access_log off;
}
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -t
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -s reload
刷新網頁,繼續查看日誌:
[root@LampLinux ~]# cat /tmp/access.log
發現沒有限制記錄的圖片了,但是還有js和css類型的圖片沒有禁止記錄。
下面我們去配置,還是進入“虛擬主機配置文件”編輯,在上一段下面繼續補充一段:
location ~ (static|cache) # 限定static和cache,因爲日誌中觀察到css和js都在這個目錄下。
{
access_log off;
}
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -t
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -s reload
刷新網頁,查看日誌:
[root@LampLinux ~]# cat /tmp/access.log
不記錄限制的圖片文件了。