ngnix之日志管理

摘要:./sbin/nginx stop./sbin/nginx reopen ./sbin/nginx -t配置文件./sbin/nginx -h日志管理[root@localhost ngnix]# tail -10 logs/access.log192.168.88.1 - - [26/Dec/2015:19:28:29 -0800] \"GET / HTTP/1.1\" 403 168 \"-\" \"M

./sbin/nginx stop
./sbin/nginx reopen
./sbin/nginx -t配置文件
./sbin/nginx -h

timg (68).jpg
日志管理
[root@localhost ngnix]# tail -10 logs/access.log
192.168.88.1 - - [26/Dec/2015:19:28:29 -0800] "GET / HTTP/1.1" 403 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:28:29 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:28:30 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:31:17 -0800] "GET / HTTP/1.1" 403 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:55:13 -0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:55:13 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:55:13 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:55:22 -0800] "GET / HTTP/1.1" 403 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:55:22 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
192.168.88.1 - - [26/Dec/2015:19:55:22 -0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
可能有蜘蛛不是火狐
www.baidu.com/robots.txt
[root@localhost ngnix]# vi conf/nginx.conf
server{
listen 80;
server_name z.com;
location /{
root z.com;
index index.html;
}
access_log logs/z.com.access.log main;
}

若出现以下错误nginx: [emerg] unknown log format "main" in /usr/local/ngnix/conf/nginx.conf:41
#log_format main \'$remote_addr - $remote_user [$time_local] "$request" \'
#\'$status $body_bytes_sent "$http_referer" \'
#\'"$http_user_agent" "$http_x_forwarded_for"\';
放开以上注释
log_format main \'$remote_addr - $remote_user [$time_local] "$request" \'
\'$status $body_bytes_sent "$http_referer" \'
\'"$http_user_agent" "$http_x_forwarded_for"\';
nginx定时切割
[root@localhost ngnix]# date -d yesterday
Tue Dec 29 18:42:19 PST 2015
[root@localhost ngnix]# date
Wed Dec 30 18:42:25 PST 2015
[root@localhost ngnix]# date -s \'2013-09-21 19:00:38\'
Sat Sep 21 19:00:38 PDT 2013
[root@localhost ngnix]# clock -w
[root@localhost ngnix]# date -d yesterday
Fri Sep 20 19:01:41 PDT 2013
[root@localhost ngnix]# date -d yesterday "%Y"
[root@localhost ngnix]# date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

-d, --date=STRING display time described by STRING, not `now\'
-f, --file=DATEFILE like --date once for each line of DATEFILE
-r, --reference=FILE display the last modification time of FILE
-R, --rfc-2822 output date and time in RFC 2822 format.
Example: Mon, 07 Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC output date and time in RFC 3339 format.
TIMESPEC=`date\', `seconds\', or `ns\' for
date and time to the indicated precision.
Date and time components are separated by
a single space: 2006-08-07 12:34:56-06:00
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time
--help display this help and exit
--version output version information and exit
[root@localhost ngnix]# date -d yesterday +%Y
2013
[root@localhost ngnix]# date -d yesterday +%Y%m%d
20130920
[root@localhost ngnix]# mkdir data
[root@localhost ngnix]# ls
client_body_temp html scgi_temp
conf logs uwsgi_temp
data proxy_temp z.com
fastcgi_temp sbin
[root@localhost ngnix]# cd data
[root@localhost data]# vi runlog.sh
#!bin/bash
LOGPATH=/usr/local/ngnix/logs/z.com.access.log
BASEPATH=/data
bak=/usr/local/ngnix/data/$(date -d yesterday +%Y%m%d%H%M).zcom.access.log
mv $LOGPATH $bak
touch $LOGPATH
kill -USR1 `cat /usr/local/ngnix/logs/nginx.pid`


[root@localhost data]# crontab -e
*/1 * * * * sh /usr/local/ngnix/data/runlog.sh
location精准匹配
[root@localhost data]# cd ..
[root@localhost ngnix]# cd ..
[root@localhost local]# pkill -9 nginx
[root@localhost local]# rm -rf ngnix/
[root@localhost local]# ls
bin etc games include lib lib64 libexec sbin share src
[root@localhost local]# cd /usr/local
[root@localhost local]# ls
bin etc games include lib lib64 libexec sbin share src
[root@localhost local]# cd src
[root@localhost src]# wget http://nginx.org/download/nginx-1.4.2.tar.gz
[root@localhost src]# tar -zxvf nginx-1.4.2.tar.gz
[root@localhost src]#cd nginx-1.4.2
[root@localhost nginx-1.4.2]#./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.4.2]#make
[root@localhost nginx-1.4.2]#make install
oot@localhost nginx-1.4.2]# vi conf/nginx.conf
43 location =/ {#加上=
44 root /var/www/html;
45 index index.html index.htm;
46 }
47 location / {
48 root html;
49 index index.html index.htm;
50 }

location 语法
location 有”定位”的意思, 根据Uri来进行不同的定位.
在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.
比如, 碰到.php, 如何调用PHP解释器? --这时就需要location
location 的语法
location [=|~|~*|^~] patt {
}
中括号可以不写任何参数,此时称为一般匹配
也可以写参数
因此,大类型可以分为3种
location = patt {} [精准匹配]
location patt{} [一般匹配]
location ~ patt{} [正则匹配]


如何发挥作用?:
首先看有没有精准匹配,如果有,则停止匹配过程.
location = patt {
config A
}
如果 $uri == patt,匹配成功,使用configA
location = / {
root /var/www/html/;
index index.htm index.html;
}

location / {
root /usr/local/nginx/html;
index index.html index.htm;
}

如果访问  http://xxx.com/
定位流程是 
1: 精准匹配中 ”/” ,得到index页为  index.htm
2: 再次访问 /index.htm , 此次内部转跳uri已经是”/index.htm” ,
根目录为/usr/local/nginx/html
3: 最终结果,访问了 /usr/local/nginx/html/index.htm


再来看,正则也来参与.
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}

location ~ image {
root /var/www/image;
index index.html;
}

如果我们访问 http://xx.com/image/logo.png
此时, “/” 与”/image/logo.png” 匹配
同时,”image”正则 与”image/logo.png”也能匹配,谁发挥作用?
正则表达式的成果将会使用.

图片真正会访问 /var/www/image/logo.png



location / {
root /usr/local/nginx/html;
index index.html index.htm;
}

location /foo {
root /var/www/html;
index index.html;
}
我们访问 http://xxx.com/foo
对于uri “/foo”, 两个location的patt,都能匹配他们
即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,
此时, 真正访问 /var/www/html/index.html
原因:’/foo’匹配的更长,因此使用之.;



url解析-》是否精准匹配如果?否 ->普通匹配