Skip to content

ngx_http_log_module

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

ngx_http_empty_gif_module

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

nginx
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

    # buffer=32k 表示日志内存缓冲区,当缓存内容达到 32k 时才写入 
    # /usr/local/nginx/access.log。
    # gzip=6 表示压缩日志,压缩比为 6。
    # flush=1s 表示 /ngx_log/nginx-access.log 内容刷新时间
    access_log /ngx_log/nginx-access.log compression buffer=32k gzip=9 flush=1s;
    # 缓存文件描述符。
    # max 表示最大的缓存数量
    open_log_file_cache max=5 inactive=10s min_uses=1 valid=60s;

    # json 格式输出
    # log_format json json '{"timestamp":"$time_iso8601",'
    #                         '"source":"$server_addr",'
    #                         '"hostname":"$hostname",'
    #                         '"remote_user":"$remote_user",'
    #                         '"ip":"$http_x_forwarded_for",'
    #                         '"client":"$remote_addr",'
    #                         '"request_method":"$request_method",'
    #                         '"scheme":"$scheme",'
    #                         '"domain":"$server_name",'
    #                         '"referer":"$http_referer",'
    #                         '"request":"$request_uri",'
    #                         '"requesturl":"$request",'
    #                         '"args":"$args",'
    #                         '"size":$body_bytes_sent,'
    #                         '"status": $status,'
    #                         '"responsetime":$request_time,'
    #                         '"upstreamtime":"$upstream_response_time",'
    #                         '"upstreamaddr":"$upstream_addr",'
    #                         '"http_user_agent":"$http_user_agent",'
    #                         '"http_cookie":"$http_cookie",'
    #                         '"https":"$https"'
    #                         '}';
    # access_log /ngx_log/nginx-access.log json buffer=32k gzip=9 flush=1s;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root html;
            index index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
cp ./nginx-access.log ./access.log.gz
# 解压缩
gzip -d ./access.log.gz

错误日志

http://nginx.org/en/docs/ngx_core_module.html#error_log

nginx
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    # 错误日志
    error_log /ngx_log/nginx-error.log;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root html;
            index index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

日志分割

  • 脚本

  • Logrotate