Nginx配置——不记录指定文件类型日志

## 查看nginx配置文件的日志格式  html

[root@aminglinux ~]# cd /usr/local/nginx/conf/vhosts/linux

[root@aminglinux vhosts]# vim ../nginx.confnginx

WEBRESOURCE1742321ec34a33951dc056b1ae3da

wyy    日志名字,能够自定义apache

$remote_addr   远程的ipvim

$http_x_forwarded   代理的ip浏览器

$time_local   时间ssh

$host   域名curl

$request_uri   访问的地址ide

$status   状态码工具

## 使用指定的日志格式

[root@aminglinux vhosts]# vim test.conf

access_log /tmp/access.log wyy;

WEBRESOURCE724fca843c2a8dc34c57be62af1c1

## 检查并从新加载

[root@aminglinux vhosts]# /usr/local/nginx/sbin/nginx -t

[root@aminglinux vhosts]# /usr/local/nginx/sbin/nginx -s reload

 

## 测试

[root@aminglinux vhosts]# curl -x127.0.0.1:80 www.test.com/sfsfsfsfs  -I

HTTP/1.1 404 Not Found

Server: nginx/1.6.2

Date: Wed, 26 Oct 2016 14:38:28 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive

## 查看生成的日志

[root@aminglinux vhosts]# ls /tmp/access.log

/tmp/access.log

[root@aminglinux vhosts]# cat /tmp/access.log

127.0.0.1 - [26/Oct/2016:22:38:28 +0800]www.test.com "/sfsfsfsfs" 404"-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

解释说明:

这说是咱们刚刚用curl测试访问产生的日志,也能够用浏览器访问一下 

访问页面,会看到好多日志,为何会这么多?由于咱们没有限制它去记录图片(png,gif等),就像apache的日志同样,能够去掉的,怎么去配置呢?

## 配置不记录指定类型日志

[root@aminglinux vhosts]# vim test.conf

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {

        access_log off;

 }

WEBRESOURCEf8dd6439eaf50ed8b174fbed3ea7d

## 检查并从新加载

[root@aminglinux vhosts]# /usr/local/nginx/sbin/nginx -t

[root@aminglinux vhosts]# /usr/local/nginx/sbin/nginx -s reload

## 为了便于测试,把以前的日志内容清空

[root@aminglinux vhosts]# >/tmp/access.log

[root@aminglinux vhosts]# cat /tmp/access.log

[root@aminglinux vhosts]#

## 去访问网页,而后再查看日志就不会看到图片了。可是仍是会有好多包含static或cache路径的一些文 件,这些也不须要,也要去作一下配置

location ~ (static|cache)

 {

       access_log off;

 }

WEBRESOURCEd88a03902373ae659726864e3319e

注:location是有前后顺序的,若是匹配的第一个就不会再匹配第二个,因此必定要区分前后顺序,放在哪里合适。

 

思考 :这个日志配置好了后,可能会一直写一直/tmp/access.log里写,假如日志不去管它,终有一天会把你的硬盘写满,因此咱们须要作一个日志的切割,天天去生成一个新的,而后把超过多少天的日志给删除,这个如何去作?(它不像apache有一个工具,nginx没有工具,须要咱们本身去写脚本)

相关文章
相关标签/搜索