• nginx日志配置,以及日志轮询


    一、为nginx配置错误日志

    Nginx错误日志是调试nginx的重要手段,属于核心功能模块的参数(ngx_core_module)该参数名字为err_log,是放在Main区块中全局配置

    err_log的语法格式以及参数语法说明如下

    err_log   file              level(级别)

    关键字           日志文件              错误日志级别

    其中关键字err_log不能改变

     1 1、在配置文件中写入error_log logs/error.log error
     2 [root@A conf]# Vim nginx.conf
     3 worker_processes  1;
     4 error_log logs/error.log error;   →这里是写入的默认log
     5 events {
     6     worker_connections  1024;
     7 }
     8 http {
     9     include       mime.types;
    10     default_type  application/octet-stream;
    11     sendfile        on;
    12     keepalive_timeout  65;
    13     #nginx vhosts config
    14     include extra/www.conf;
    15     include extra/bbs.conf;
    16     include extra/blog.conf;
    17     include extra/status.conf;
    18 }
    19 2、检查语法,并且重启
    20 3、查看日志,如果日志太多可以清空日志然后查看
    21 [root@A conf]# cat ../error.log   →查看nginx的错误日志文件
    22 
    23 [root@A conf]# > ../logs/error.log   →这个是日志太多然后看不清的时候清空日志
    24 [root@A conf]# cat ../logs/error.log  →之后查看日志

    二、访问日志作用以及配置

    所有的web软件都有这样的功能,会把每个用户访问网站的日志信息记录到指定的日志文件里面,供网站提供这分析用户浏览行为等,如百度。此功能由nginx_http_log_module模块负责,对应的官方地址:

    http://nginx.org/en/docs/http/ngx_http_log_module.html

    Nginx 访问日志参数

    log_format   用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)

    access_log   用来指定日志文件的路径及使用的何种日志格式记录日志

    用法如下:

    1、取出日志参数格式

    下面的log_format格式,在vim nginx_default里面21到23行

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                          '$status $body_bytes_sent "$http_referer" '

                          '"$http_user_agent" "$http_x_forwarded_for"';

    下面是定义参数

    $http_x_forwarded_for  代理接收客户端信息

    2、把参数放到配置的nginx.conf里面
    Vim nginx.conf
    
    worker_processes  1;
    error_log logs/error.log error;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' →这里放参数
                          '$status $body_bytes_sent "$http_referer" '   
                          '"$http_user_agent" "$http_x_forwarded_for"';        →放参数
        #nginx vhosts config
        include extra/www.conf;
        include extra/bbs.conf;
        include extra/blog.conf;
        include extra/status.conf;
    }

    配置访问日志如下:

    默认配置:access_log logs/access.log cimbined;  

    放置位置:http,server,location,if in location limit_except

    access_log 访问位置,这里一般在虚拟主机里面

    logs/access.log 记录文件

    cimbined; 以什么样的格式记录

    1、    写到虚拟主机里面,这里先写到www.conf里面
     [root@A conf]# vim extra/www.conf
    server {
            listen       80;
            server_name  www.cnblog.co pyrene;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
            access_log logs/access_www.log main;    -→放到location外面,logs是安装目录,后面是名字,以main的方式,这样才你能和log_format 结合使用
        }
    2、    检查语法/application/nginx/sbin/nginx –t,重启
    3、    查看日志变化用 tail -f /application/nginx/logs/access_www.log   这里的是上面起名字防止的位置

    1、  浏览器访问绑定的ip地址就会出现如下:并且和access_log对比

    定义的格式

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                          '$status $body_bytes_sent "$http_referer" '

                          '"$http_user_agent" "$http_x_forwarded_for"';

    里面有参数referer,可以显示你从那个页面过来的是什么手机,什么系统等等

    真实的格式

    192.168.197.1 - - [17/Dec/2

    016:06:42:14 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/5.0.2.200 Chrome/47.0.2526.73 Safari/537.36" "-"

    详细说明信息:

    上面的还可以优化,如下

    三、日志轮询

    nginx中的日志轮询切割一般用cron+脚本

    方法如下:

    1、首先写入脚本

    [root@oldboy logs]# mkdir /server/scripts -p
    [root@oldboy logs]# cd /server/scripts/
    写脚本之前先创建脚本存放目录
    [root@oldboy scripts]# vim cut_nginx_log.sh 
    #!/bin/bash
    cd /application/nginx/logs
    #这里如果有博客的log那就把log在这里mv
    /bin/mv access_www.log  www_access_$(date +%F -d '-1day').log
    /application/nginx/sbin/nginx -s reload
    #下面可以把这个推送到备份服务器然后删除七天之前的

    2、执行 /bin/sh /server/scripts/cut_nginx_log.sh

    3、查看是否生成log

    [root@oldboy logs]# ls
    access.log access_www.log error.log nginx.pid www_access_2017-03-03.log

    4、定时任务

    [root@oldboy logs]# crontab -l
    #time sync by pyrene 2017-1-16
    */5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
    # nginx acess_log
    00 * * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

  • 相关阅读:
    C#实现图片的无损压缩
    C#实现图片的无损压缩
    ACM2034
    产品经理入门攻略(三)
    编程思想14.类型信息
    分布式ID生成策略 · fossi
    在加拿大找工作:如何写简历(适用理工科)
    支持向量机 SVM
    javaSE复习之——线程
    spring基于@Value绑定属Bean性失
  • 原文地址:https://www.cnblogs.com/pyrene/p/6502132.html
Copyright © 2020-2023  润新知