Nginx演练(2)配置日志访问

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

在互联网企业中,日志分析是特别重要的一个模块。通过对日志的分析,可以获得网站的一些指标信息,和网站访问情况。另外,日志对于统计排错来说非常有利的。

nginx日志相关的配置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。

日志相关的配置参数

1.access_log指令

1.1语法

1
2
3
4
5
6
7
8
9
10
11
12
13
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
 
默认值: access_log logs /access .log combined;
配置段: http, server, location,  if  in  location, limit_except
gzip 压缩等级。
buffer设置内存缓存区大小。
flush保存在缓存区中的最长时间。
不记录日志:access_log off;

1.2 例子

1
access_log  /path/to/log .gz combined  gzip  flush=5m;

2. log_format指令

2.1语法

1
2
3
4
5
6
7
Syntax:    log_format name string ...;
Default:    
log_format combined  "..." ;
Context:    http
 
name表示格式名称,string表示等义的格式。
log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式。

2.2 例子

1
2
3
log_format combined  '$remote_addr - $remote_user [$time_local] '
                     '"$request" $status $body_bytes_sent '
                     '"$http_referer" "$http_user_agent"' ;


$bytes_sent
the number of bytes sent to a client
$connection
connection serial number
$connection_requests
the current number of requests made through a connection (1.1.18)
$msec
time in seconds with a milliseconds resolution at the time of the log write
$pipe
“p” if request was pipelined, “.” otherwise
$request_length
request length (including request line, header, and request body)
$request_time
request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client
$status
response status
$time_iso8601
local time in the ISO 8601 standard format
$time_local
local time in the Common Log Format


3. open_log_file_cache指令

3.1 语法

1
2
3
4
5
6
7
8
9
10
11
12
13
Syntax:open_log_file_cache max=N [inactive= time ] [min_uses=N] [valid= time ];
open_log_file_cache off;
     
Default:open_log_file_cache off;
     
Context:http, server, location    
对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off),格式如下:
参数注释如下:
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
valid:设置检查频率,默认60s
off:禁用缓存

3.2 例子

1
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

4. log_not_found指令

4.1 语法

1
2
3
4
5
6
Syntax:log_not_found on | off;
     
Default:log_not_found on;
     
Context:http, server, location
是否在error_log中记录不存在的错误。默认是。

5. log_subrequest指令

语法: log_subrequest on | off;
默认值: log_subrequest off;
配置段: http, server, location
是否在access_log中记录子请求的访问日志。默认不记录。

6. rewrite_log

由ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启。 Nginx重写规则指南
语法: rewrite_log on | off;
默认值: rewrite_log off;
配置段: http, server, location, if
启用时将在error log中记录notice级别的重写日志。

7. error_log指令

Syntax:    error_log file [level];
Default:    
error_log logs/error.log error;
Context:    main, http, mail, stream, server, location

语法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
默认值: error_log logs/error.log error;
配置段: main, http, server, location
配置错误日志。


nginx配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
http {
     include       mime.types;
     default_type  application /octet-stream ;
 
     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  logs /access .log  main;
 
     sendfile        on;
     #tcp_nopush     on;
 
     #keepalive_timeout  0;
     keepalive_timeout  65;
 
     server {
         # 监听的IP和端口
         listen 192.168.163.146:80;
         server_name server1.domain.com;
         access_log logs /server1 .access.log main;
 
         location /
         {
             index index.html index.htm;
             #存放目录
             root  /u01/up1 ;
         }
     }
 
}
cat  /usr/local/nginx-1 .7.9 /logs/server1 .access.log
1
2
3
4
5
6
7
8
9
192.168.163.1 - - [29 /Aug/2016 :14:32:00 +0800]  "GET / HTTP/1.1"  200 134  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"
192.168.163.1 - - [29 /Aug/2016 :14:32:01 +0800]  "GET / HTTP/1.1"  200 134  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"
192.168.163.1 - - [29 /Aug/2016 :14:32:01 +0800]  "GET / HTTP/1.1"  200 134  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"
...
192.168.163.1 - - [29 /Aug/2016 :14:32:03 +0800]  "GET / HTTP/1.1"  200 134  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"
192.168.163.1 - - [29 /Aug/2016 :14:32:04 +0800]  "GET / HTTP/1.1"  200 134  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"
192.168.163.1 - - [29 /Aug/2016 :14:32:41 +0800]  "GET /index.html HTTP/1.1"  200 134  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"
192.168.163.1 - - [29 /Aug/2016 :14:32:48 +0800]  "GET /index2html HTTP/1.1"  404 168  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"
192.168.163.1 - - [29 /Aug/2016 :14:32:51 +0800]  "GET /index2.html HTTP/1.1"  404 168  "-"  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0"  "-"

整体来说,日志配置非常简单。接下来完成日志切割。

为什么要进行日志切割?

我等苦逼程序员,都有过解决线上BUG的经验吧。如果一个日志文件过大,无论从维护或查看角度来说,都不方便。所以要将日志进行切割,而按日切割是最常见的一种方式。这有点像log4j中的某个appender[org.apache.log4j.DailyRollingFileAppender].

按日切割逻辑很简单。主要分3步。

  1. 备份日志到新文件

  2. 重新启动nginx

  3. 将命令加入cron调度

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
 
log_path= "/usr/local/nginx-1.7.9/logs/"
 
pid_path= "/usr/local/nginx-1.7.9/conf/nginx.pid"
 
mkdir  -p ${log_path}$( date  -d  "yesterday"  + "%Y" )/$( date  -d  "yesterday"  + "%m" )/
 
mv  ${log_path}server1.access.log ${log_path}$( date  -d  "yesterday"  +%Y)/$( date  -d  "yesterday"  +%m) /server1 .access_$( date  -d  "yesterday"  +%Y%m%d).log
 
kill   -USR1 ` cat  ${pid_path}`

笔者对linux不经常写,平时也就是简单玩玩。写上面代码也是参照了网上或书中的例子。结果充满坎坷。

1.由于我使用WINSCP工具原因,在本地使用sublime工具开发,结果出现错误

shell /bin/bash^M: bad interpreter错误解决

2.中间错误将$(date -d "yesterday" +"%Y" 写成 ${date -d "yesterday" +"%Y"}

3.将kill  -USR1 错误写成“kill  -USER1”


Nginx控制信号

TERM, INT

快速关闭

QUIT

从容关闭

HUP

重新加载,用新的配置开始新的工作进程

USER1

重新打开日志文件

USER2

平滑升级可执行程序

WINCH

从容关闭工作进程

 

()会开启一个新的子shell,{}不会开启一个新的子shell
(())常用于算术运算比较,[[]]常用于字符串的比较.
$()返回括号中命令执行的结果
$(())返回的结果是括号中表达式值
${ }参数替换与扩展





本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1843959,如需转载请自行联系原作者
相关实践学习
通过日志服务实现云资源OSS的安全审计
本实验介绍如何通过日志服务实现云资源OSS的安全审计。
相关文章
|
6月前
|
SQL Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— application.yml 中对日志的配置
在 Spring Boot 项目中,`application.yml` 文件用于配置日志。通过 `logging.config` 指定日志配置文件(如 `logback.xml`),实现日志详细设置。`logging.level` 可定义包的日志输出级别,例如将 `com.itcodai.course03.dao` 包设为 `trace` 级别,便于开发时查看 SQL 操作。日志级别从高到低为 ERROR、WARN、INFO、DEBUG,生产环境建议调整为较高级别以减少日志量。本课程采用 yml 格式,因其层次清晰,但需注意格式要求。
567 0
|
2月前
|
JSON 安全 Go
Go语言项目工程化 —— 日志、配置、错误处理规范
本章详解Go语言项目工程化核心规范,涵盖日志、配置与错误处理三大关键领域。在日志方面,强调其在问题排查、性能优化和安全审计中的作用,推荐使用高性能结构化日志库zap,并介绍日志级别与结构化输出的最佳实践。配置管理部分讨论了配置分离的必要性,对比多种配置格式如JSON、YAML及环境变量,并提供viper库实现多环境配置的示例。错误处理部分阐述Go语言显式返回error的设计哲学,讲解标准处理方式、自定义错误类型、错误封装与堆栈追踪技巧,并提出按调用层级进行错误处理的建议。最后,总结各模块的工程化最佳实践,助力构建可维护、可观测且健壮的Go应用。
|
5月前
|
存储 监控 API
【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息
本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。
158 23
|
6月前
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
|
6月前
|
数据库连接 测试技术 Windows
【YashanDB知识库】windows配置ODBC跟踪日志, 使用日志定位问题
【YashanDB知识库】windows配置ODBC跟踪日志, 使用日志定位问题
|
应用服务中间件 Apache nginx
nginx自己写日志切割脚本
转自本人的博客:http://blog.teier.cn
1539 0
|
监控 应用服务中间件 nginx
|
应用服务中间件 nginx Perl
|
应用服务中间件 nginx Shell
|
Shell 应用服务中间件 nginx