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

一:nginx访问日志

日志格式
vim /usr/local/nginx/conf/nginx.conf //搜索log_format
$remote_addr 客户端ip(公网ip)
$http_x_forwarded_for 代理服务器的ip
$time_local 服务器本地时间
$host 访问主机名(域名)
$request_uri 访问的url地址
$status 状态码
$http_referer referer
$http_user_agent user_agent
除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机里配置
access_log /tmp/1.log combined_realip;
这里的combined_realip就是在nginx.conf中定义的日志格式名字
-t$$-s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log

二:nginx日志切割

  1. nginx日志切割脚本
    首先确定访问日志路径,假定为/home/logs/www_access.log,还要确定nginx 的pid文件所在路径,假定为/usr/local/nignx/var/nignx.pid
    vim /usr/local/sbin/nignx_logrotate.sh
    加入以下内容
    #!/bin/bash
    d='date -d "-1 day" +&%F<br/>/bin/mv /home/logs/www_access.log /home/logs/$d_www_access.log<br/>/etc/init.d/nginx reload &gt; /dev/null<br/>gzip -f $d_www_access.log #如果日志比较大,进行压缩<br/>/bin/kill -HUPcat /usr/local/nginx/var/nginx.pid` #重新加载以下配置文件

  2. 借住系统的logrotate工具实现
    vim /etc/logrotate.d/nginx
    加入以下内容
    /home/logs/*.log {
    Daily
    Missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 644 nobody nobody
    sharedscripts
    postrotate
    [ -f /usr/local/nginx/var/nignx.pid ]&&kill -USR1 cat /usr/local/nginx/var/nginx.pid
    Endscript
    }
    说明:
    第一行就要定义日志的路径,可以是多个路径
    daily 表示日志按天归档
    missingok 表示忽略所有错误,比如日志文件不存在的情况下
    rotate 52 表示存放日志的数量最多52个,最老的会被删除
    compress 表示日志要压缩
    delaycompress 表示压缩除了当前和最近之外的所有其他版本
    notifempty 表示如果日志为空,则不归档
    create 644 nobody nobody 定义归档日志的权限以及属主和属组
    sharedscript 表示结束了

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

    配置如下
    location ~ ..(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 7d;
    access_log off;
    }
    location ~ .
    .(js|css)$
    {
    expires 12h;
    access_log off;
    }

猜你喜欢

转载自blog.51cto.com/10941098/2160036