**
**
nginx系列之一:nginx入门 nginx系列之二:配置文件解读 nginx系列之三:日志配置 nginx系列之四:web服务器 nginx系列之五: 负载均衡 nginx系列之六:cache服务 nginx系列之七:限流配置 nginx系列之八:使用upsync模块实现负载均衡
转自:在此感谢原博主的整理分享
access_log用来定义日志级别,日志位置。语法如下: 日志级别: debug > info > notice > warn > error > crit > alert > emerg
语法格式: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off;
默认值 : access_log logs/access.log combined; 作用域 : http, server, location, if in location, limit_except
实例一:
access_log /var/logs/nginx-access.log compression buffer=32k; 1语法格式: log_format name [escape=default|json] string …; 默认值 : log_format combined “…”; 作用域 : http
实例一:
log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; access_log /var/logs/nginx-access.log compression buffer=32k; 1234使用open_log_file_cache来设置日志文件缓存(默认是off)。
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。inactive:设置存活时间,默认是10smin_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次valid:设置检查频率,默认60soff:禁用缓存语法格式: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; 默认值: open_log_file_cache off; 作用域: http, server, location
实例一
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2; 1当你设置日志级别成 debug,如果你在调试一个在线的高流量网站的话,你的错误日志可能会记录每个请求的很多消息,这样会变得毫无意义。
在events{...}中配置如下内容,可以使 Nginx 记录仅仅来自于你的 IP 的错误日志。
events { debug_connection 1.2.3.4; } 123调试rewrite规则时,如果规则写错只会看见一个404页面,可以在配置文件中开启nginx rewrite日志,进行调试。
server { error_log /var/logs/nginx/example.com.error.log; rewrite_log on; } 1234rewrite_log on; 开启后,它将发送所有的 rewrite 相关的日志信息到 error_log 文件中,使用 [notice] 级别。随后就可以在error_log 查看rewrite信息了。
配置以上配置后,/static/ 相关的日志会被单独记录在static-error.log文件中。
nginx日志共三个参数
access_log: 定义日志的路径及格式。log_format: 定义日志的模板。open_log_file_cache: 定义日志文件缓存。proxy_set_header X-Forwarded-For :如果后端Web服务器上的程序需要获取用户IP,从该Header头获取。proxy_set_header X-Forwarded-For $remote_addr;
坑点: 使用$uri 可以在nginx对URL进行更改或重写,但是用于日志输出可以使用$request_uri代替,如无特殊业务需求,完全可以替换。
日志中增加了压缩的信息。
http { log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; server { gzip on; access_log /spool/logs/nginx-access.log compression; ... } } 12345678910增加upstream消耗的时间。
http { log_format upstream_time '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"' 'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"'; server { access_log /spool/logs/nginx-access.log upstream_time; ... }}
123456789101136461 200 483 500 87 404 9 400 3 302 1 499 1 403 1 301
12345678910How to Configure Custom Access and Error Log Formats in Nginx 如何在Nginx中配置自定义访问和错误日志格式