菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
195
0

④nginx日志管理

原创
05/13 14:22
阅读数 93813

nginx程序日志配置说明

1.log_format定义日志格式语法

 Syntax:      log_format name [escape=default|json|none] string ...;   #name是变量名
Default:      log_format combined "...";
Context:      http                                                     #只能配置http区域 

2.默认Nginx定义语法格式如下

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';
	 access_log  /var/log/nginx/access_oldboy.log  main;
	   nginx默认变量:
	   $remote_addr:    --- 访问网站客户端源IP地址
	   $remote_user      --- 表示认证用户名称信息
	   [$time_local]     --- 显示响应时间信息
	   $request          --- 显示请求行信息
	   $status           --- 显示状态码
	   $body_bytes_sent  --- 回复数据大小(流量)信息 字节
	   $http_referer     --- 盗链人地址信息/用于推广
	   $http_user_agent  --- 显示用户访问网站客户端程序 (电脑 手机)
           $request_time     --- 请求花费的时间  单位为妙  精度毫秒
           $http_x_forwarded_for  ---http请求携带的http信息

访问日志说明

access_log参数

Syntax: 	access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
            access_log off;
Default: 	access_log logs/access.log combined;
Context: 	http, server, location, if in location, limit_except

使用

server {
        listen       80;
        server_name  software.yangyijing.cn;
        access_log   /var/log/nginx/software_access.log software ; 
        error_log /var/log/nginx/software_error.log warn;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex on;
        charset    UTF-8;
        root   /data/software;
        location /code {   #访问uri是code的不记录到访问日志
         root /data/software;
         access_log off;
  }
}

我自己的网站使用示例

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  software  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_user_agent"';

    access_log  /var/log/nginx/access.log  main;
    server {
        listen       80;
        server_name  software.yangyijing.cn;
        access_log   /var/log/nginx/software_access.log software ; 
   错误日志:记录服务常见错误
   01. 服务运行错误信息
   02. 用户访问页面的错误
   如何配置:
   error_log  /var/log/nginx/error.log warn(记录错误级别);
   debug    --- 调试级别    产生的日志信息最多
   info     --- 信息级别
   notice   --- 通知级别 
   warn     --- 警告级别
   error    --- 错误级别  
   crit     --- 严重级别
   alert    --- 非常严重级别
   emerg    --- 灾难级别    产生的日志信息最少

nginx日志切割

cat /etc/logrotate.d/nginx 
/var/log/nginx/*log {    #/var/log/nginx/下的log文件都做日志切割
    create 0664 nginx root #切割后日志权限
    daily            #每天切割日志
    rotate 10        #日志保留10天
    missingok        #日志丢失忽略
    notifempty       #不切割空日志
    compress         #日志文件压缩
    sharedscripts    #
    postrotate       #切割日志执行的命令
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

文件查看

zcat /var/log/nginx/blog_access.log-20210407.gz

发表评论

0/200
195 点赞
0 评论
收藏