Nginx访问日志、日志切割、静态文件不记录日志和过期时间

Nginx日志配置与优化
本文介绍Nginx日志格式配置方法,包括如何定义日志格式、实现日志切割,以及如何针对静态文件进行日志排除及设置过期时间等优化措施。

12.10 Nginx访问日志

  • 日志格式
#搜索log_format
[root@taoyuan ~]# vim /usr/local/nginx/conf/nginx.conf

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

  • 这里的combined_realip就是在nginx.conf中定义的日志格式名字
参数含义
$remot_addr客户端IP(公网IP)
$http_x_forwarded_for代理服务器的IP
$time_local服务器本地时间
$host访问主机名(域名)
$request_uri访问的url地址
$status状态码
$http_refererreferer
$http_user_agentuser_agent
  • 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加如下的参数
[root@taoyuan ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 
#必须根Nginx.conf 中的日志名字是一致的

Nginx访问日志、日志切割、静态文件不记录日志和过期时间
注:taoyuan为日志格式的名字

  • -t && -s reload
[root@taoyuan ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown log format "taoyuan" in /usr/local/nginx/conf/vhost/test.com.conf:10
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@taoyuan ~]# /usr/local/nginx/sbin/nginx -s reload
nginx: [emerg] unknown log format "taoyuan" in /usr/local/nginx/conf/vhost/test.com.conf:10
  • curl 测试
[root@taoyuan ~]# curl -x127.0.0.1:80 test4.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 13:22:44 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@taoyuan ~]# curl -x127.0.0.1:80 test3.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 13:23:02 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html

[root@taoyuan ~]# curl -x127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 13:23:10 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html
  • 查看日志
[root@taoyuan ~]# cat /tmp/test.com.log 
127.0.0.1 - [04/Jan/2018:21:23:02 +0800] test3.com "/admin/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:21:23:10 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"

12.11 Nginx日志切割

  • 自定义shell日志切割脚本
#创建shell脚本
[root@taoyuan ~]# vim /usr/local/sbin/nginx_log_rotate.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`

#可以执行以下脚本
[root@taoyuan ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180103
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180103
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1055
  • 加任务计划
[root@taoyuan ~]# crontab -e
#加入如下内容
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

#在/tmp/目录下创建 test.com.log-20180109 已时间结尾的日志文件

12.12 静态文件不记录日志和过期时间

  • 配置如下
#修改虚拟主机配置文件
[root@taoyuan local]# cd /usr/local/nginx/conf/vhost/
[root@taoyuan vhost]# ls
aaa.com.conf  test.com.conf
[root@taoyuan vhost]# vim test.com.conf 

#内容
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
   {
       expires     7d;
       access_log off;
   }
   location ~ .*\.(js|css)$
   {
       expires     12h;
       access_log off;
   }

截图如下:

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

  • -t && -s reload
[root@taoyuan ~]# /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@taoyuan ~]# /usr/local/nginx/sbin/nginx -s reload
  • 测试
[root@taoyuan ~]# cd /data/wwwroot/test.com/

[root@taoyuan test.com]# vim 1.gif
[root@taoyuan test.com]# vim 2.js
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/1.gif
1.gif
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/2.js
2.js
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@taoyuan test.com]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:04:11 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@taoyuan test.com]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:04:11 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:22:04:33 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

#测试过期时间
[root@taoyuan test.com]# curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:06:15 GMT
Content-Type: application/javascript
Content-Length: 5
Last-Modified: Thu, 04 Jan 2018 14:03:28 GMT
Connection: keep-alive
ETag: "5a4e3430-5"
Expires: Fri, 05 Jan 2018 02:06:15 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
#在配置文件中 expires 是控制过期时间的,如果注释掉 -I将不显示

转载于:https://blog.51cto.com/3622288/2057556

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值