zoukankan      html  css  js  c++  java
  • 0811Nginx访问日志设置

    12.10 Nginx访问日志

    日志格式

    vim /usr/local/nginx/conf/nginx.conf   搜索log_format定义名字,后面定义下面这些

    $remote_addr

    客户端IP(公网IP)百度搜ip可以查到

    $http_x_forwarded_for

    代理服务器的IP,考试模拟网站就是代理ip

    $time_local

    服务器本地时间

    $host

    访问主机名(域名)

    $request_uri

    访问的url地址

    $status

    状态码,301404

    $http_referer

    referer

    $http_user_agent

    user_agent

     除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加访问日志路径

     vim /usr/local/nginx/conf/vhost/test.com.conf

     access_log /tmp/test.com.log这就是虚拟主机日志地址 ,加到任意一行

     

     这里的access_log /tmp/test.com.log quyifan

     就是在/usr/local/nginx/conf/nginx.conf中定义的日志格式名字quyifan

     /usr/local/nginx/sbin/nginx -t

     /usr/local/nginx/sbin/nginx -s reload

     curl -x127.0.0.1:80 test.com -I

     curl -x127.0.0.1:80 test2.com -I

     cat /tmp/test.com.log 查看上面日志

     

    日志只记录了成功登陆的信息,任何域名都跳转到test.com,但是乱写的不记录在日志

    12.11 Nginx日志切割

    没有自带切割工具,可以写一个日志切割脚本

    这里自定义shell脚本

     vim /usr/local/sbin/nginx_log_rotate.sh  shell脚本保存在这里

    配置如下

    #! /bin/bash d=`date -d "-1 day" +%Y%m%d` #定义切割时间(切割一天前的日志) logdir="/tmp/" #此处指定要切割的日志路径(该路径来自虚拟主机配置文件) nginx_pid="/usr/local/nginx/logs/nginx.pid" #调用pid的目的是执行命令:/bin/kill -HUP `cat $nginx_pid` #该命令等价于命令:nginx -s reload(重新加载文件),确保与虚拟主机配置文件变更保持同步 #该地址来自nginx配置文件 cd $logdir for log in `ls *.log` do    mv $log $log-$d done #此处使用通配进行循环,对所有复合条件的日志文件进行切割 /bin/kill -HUP `cat $nginx_pid` #执行此命令进行重载生成新的日志文件来记录新的日志

     执行该脚本,-x显示脚本执行过程:

    sh -x /usr/local/sbin/nginx_log_rotate.sh

     任务计划:

     0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

    删除30天前的日志 find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm

     

    这里日志创建好,今天时间没到还没切割,所以只有test.com.log有信息

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

    vim /usr/local/nginx/conf/vhost/test.com.conf

    配置如下

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$   正则

        {

              expires      7d;  (上下写一起是因为设置的过期时间不同)

              access_log off;

        }

    location ~ .*.(js|css)$

        {

              expires      12h;

              access_log off;

        }

    location指定静态文件 expires指定过期时间 access_log配置成off就代表不记录日志

     /usr/local/nginx/sbin/nginx -t

     /usr/local/nginx/sbin/nginx -s reload

    测试:加两个后缀为1.gif2.js的文件

    echo "11111111111" > /data/wwwroot/test.com/1.gif

    echo "22222222222" > /data/wwwroot/test.com/2.js

    touch /data/wwwroot/test.com/1.jss

    curl -x127.0.0.1:80 test.com/1.gif

    curl -x127.0.0.1:80 test.com/2.js

    curl -x127.0.0.1:80 test.com/1.jss

    然后查看日志 cat /tmp/test.com.log 没有记录gifjs的日志

     

    最后日志没有记录jpgjs,记录了错误的404jss,实验成功

    jpgjs的过期时间不同,查看如下

     

  • 相关阅读:
    spring-session+Redis实现Session共享
    SQLServer语法常用总结
    [PDFBox]后台操作pdf的工具类
    类加载器
    SQLServer常用分页方式
    Tesseract识别图片提取文字&字库训练
    AbstractQueuedSynchronizer的简单介绍
    CountDownLatch 闭锁、FutureTask、Semaphore信号量、Barrier栅栏
    Java线程实现的第三种方式Callable方式与结合Future获取返回值
    原子类型的使用&Unsafe&CAS
  • 原文地址:https://www.cnblogs.com/0329linux/p/7371382.html
Copyright © 2011-2022 走看看