[toc]javascript
vim /usr/local/nginx/conf/vhost/../nginx.confphp
[root@xavi vhost]# vim ../nginx.conf
找到以下,是定義日誌格式:css
log_format xavi '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
combined_realip;爲日誌格式名字,能夠改成其餘的,後面能夠調用它 上述定義日誌的名稱改成xavi。 注:這邊改爲什麼名字,待會引用的時候就寫成什麼!html
如上除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件中增長:java
access_log /tmp/atorreid.log xavi; 使用access_log指定日誌存儲路徑和使用的日誌格式名字nginx
[root@xavi vhost]# vim atorreid.com.conf server { listen 80 default_server; server_name atorreid.com xavi.com abc.com; index index.html index.htm index.php; root /data/nginx/www.torreid.com; if ($host != 'torreid.com' ) { rewrite ^/(.*)$ http://torreid.com/$1 permanent; } access_log /tmp/test.com.log xavi location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
[root@xavi vhost]# /usr/local/nginx/sbin/nginx -s reload [root@xavi vhost]# !curl curl -x127.0.0.1:80 www.atorreid.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Wed, 14 Mar 2018 16:05:55 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://torreid.com/index.html
日誌對於統計排錯來講很是有利的,若是一個100G的日誌別說查看了就打開咱們都須要等待好久這樣不只浪費了咱們的硬件資源同時也浪費了時間。若是按照天天分紅一個日誌,是否是更有利於咱們去排障呢?-- by ZDYshell
因爲Nginx不像Apache有本身的切割工具,在此咱們須要寫個腳本完成需求:vim
養成好的習慣把腳本放在sbin目錄下:緩存
[root@xavi ~]# vim /usr/local/sbin/nginx_logrotate.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`
d=date -d "-1 day" +%Y%m%d;生成昨天的日期 for log in
ls *.log
do mv $log $log-$d done 這是一個for循環,把ls列舉的log文件,執行以日期格式的重命名 nginx_pid=」/usr/local/nginx/logs/nginx.pid」;就是爲了最後一行而設定的。bash
/bin/kill -HUP
cat $nginx_pid
最後一行的意思和以前使用的 -s reload 是一個意思 重載nginx.pid,而後就會再次生成一個新的日誌文件。不然不生成日誌文件
[root@xavi ~]# sh -x /usr/local/sbin/nginx_logrotate.sh ++ date -d '-1 day' +%Y%m%d + d=20180314 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls atorreid.com.log ' php_errors.log' php_errors.log test.com.log torrecid.com.log + for log in '`ls *.log`' + mv atorreid.com.log atorreid.com.log-20180314 + for log in '`ls *.log`' + mv php_errors.log php_errors.log-20180314 + for log in '`ls *.log`' + mv php_errors.log php_errors.log-20180314 mv: 沒法獲取"php_errors.log" 的文件狀態(stat): 沒有那個文件或目錄 + for log in '`ls *.log`' + mv test.com.log test.com.log-20180314 + for log in '`ls *.log`'
有了切割能夠知足咱們的平常工做須要,可是隨着訪問量的劇增,若是不刪除老的日誌文件咱們的磁盤很快就會佔用完。
[root@xavi ~]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
crontab -e
加入以下內容 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
[root@xavi ~]# crontab -e no crontab for root - using an empty one 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
轉載自老七博客:http://www.okay686.cn/524.html
#! /bin/bash d=`date -d "-1 day" +%Y%m%d` logdir="/usr/local/php-fpm/var/log/" nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir for log in `find . -name "*_slow.log"` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid` find . -name "*_slow.log-*" -mtime +30 | xargs rm -rf
[root@xavi ~]# cd /usr/local/nginx/conf/vhost/ [root@xavi vhost]# ls atorreid.com.conf bcd.com.conf test.com.conf [root@xavi vhost]# vim test.com.conf server { listen 80 default_server; server_name test.com xavi.com abc.com; index index.html index.htm index.php; root /data/nginx/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 xavi; }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } ~匹配.gif .jpg .jpeg等格式的靜態文件不計入日誌,|表示或者 \表示脫義字符,XXX.gif
[root@xavi vhost]# cd /data/nginx/test.com/ [root@xavi test.com]# ls admin admin.php index.html [root@xavi test.com]# vim 1.gif [root@xavi test.com]# vim 2.js
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/2.js axccdecdcece: [root@xavi test.com]# curl -x127.0.0.1:80 test.com/1.gif dadadsdsda [root@xavi test.com]# curl -x127.0.0.1:80 test.com/x.gif <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@xavi test.com]# cat /tmp/test.com.log 127.0.0.1 - [15/Mar/2018:21:09:12 +0800] www.atorreid.com "/index.html" 301 "-" "curl/7.29.0"
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/xx2.jsdsdsad <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@xavi test.com]# cat /tmp/test.com.log 127.0.0.1 - [15/Mar/2018:21:09:12 +0800] www.atorreid.com "/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 - [15/Mar/2018:21:12:25 +0800] test.com "/xx2.jsdsdsad" 404 "-" "curl/7.29.0" 127.0.0.1 - [15/Mar/2018:21:15:33 +0800] test.com "/2.jsdsdsad" 404 "-" "curl/7.29.0"
[root@xavi test.com]# curl -x127.0.0.1:80 test.com/2.js -I //訪問js類型的文件,緩存過時時間爲12小時 HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Thu, 15 Mar 2018 13:17:07 GMT Content-Type: application/javascript Content-Length: 14 Last-Modified: Thu, 15 Mar 2018 13:08:00 GMT Connection: keep-alive ETag: "5aaa7030-e" Expires: Fri, 16 Mar 2018 01:17:07 GMT Cache-Control: max-age=43200 Accept-Ranges: bytes