Nginx日志配置(Ngx_http_log_module)
2018-03-22 00:47
477 查看
Ngx_http_log_module:定义日志格式,并且以指定的格式保存
官方文档:http://nginx.org/en/docs/http/ngx_http_log_module.html相关指令:(Syntax:使用语法,Default:默认,Context:默认配置段)
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 |
Syntax | log_format name [escape=default|json] string …; |
---|---|
Default | log_format combined “…”; |
Context | http |
#示例配置 log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"'; access_log /spool/logs/nginx-access.log compression buffer=32k; #access_log:访问日志文件路径; #buffer=32;缓冲在内存中,32K的内存空间,日志先缓冲至内存中,根据时间节点,空间节点在存储至硬盘中(效率更高);
变量定义:多数为Nginx内建变量
$remote_addr:客户端地址;
$remote_user:客户端用户;
[$time_local]:收到用户请求时服务器本地时间;
$reques:请求的url;
$status:响应码;
$bytes_sent:发送客户端的响应报文字节数;
$http_referer:从什么地方跳转到当前页面资源;
$http_user_agent:客户端浏览器的类型;
$gzip_ratio:页面资源压缩比;
……
缓存所打开的日志文件的元数据:
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 |
max=N:设置缓存中描述符的最大数量;
inactive=time:设置缓存描述符关闭的时间(定义非活动时长,如果在此期间使用次数小于最小使用次数,定义非活动项目)默认10s;
min_uses=N:在由inactive参数定义的时间内文件使用的最小次数;
简单来讲:在inactive=time时间内,缓存项最少访问min_uses=N次,为活动项,否则为非活动;
valid=time:设置文件检查(每隔多久检查一次缓存项是否有效);
off:禁用缓存;
演示环境:
Server:192.168.47.140 [root@GaoServer ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@GaoServer ~]# uname -r 3.10.0-327.el7.x86_64 [root@GaoServer ~]# nginx -V nginx version: nginx/1.10.2 ......
相关配置/参数:(打开日志缓存加速日志性能)
#在http配置段,定义日志参数;注意,如果在server配置段中也同样定义日志参数,以server配置段中参数生效(最小定义); [root@GaoServer ~]# vim /etc/nginx/nginx.conf http { ...... log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #main定义格式 '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; ...... #定义不同虚拟主机使用不同的访问日志: [root@GaoServer ~]# vim /etc/nginx/conf.d/server.conf server { listen 80; server_name www.server1.com; access_log /var/log/nginx/nginx_log/server1_access.log main; location / { root /data/nginx/server1; } error_page 404 =200 /404.html; location = /404.html { root /etc/nginx/error_pages/; } } server { listen 8080; server_name www.server2.com; access_log /var/log/nginx/nginx_log/server2_access.log main; location / { root /data/nginx/server2; } } #创建日志存放文件上级目录: [root@GaoServer]# mkdir /var/log/nginx/nginx_log -pv mkdir: 已创建目录 "/var/log/nginx/nginx_log" [root@GaoServer ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@GaoServer ~]# nginx -s reload #访问测试: [root@GaoServer ~]# 4000 curl 127.0.0.1 server1 [root@GaoServer ~]# curl 127.0.0.1:8080 server2 ...... [root@GaoServer ~]# cd /var/log/nginx/nginx_log/ [root@GaoServer nginx_log]# ls server1_access.log server2_access.log [root@GaoServer nginx_log]# tail server2_access.log 127.0.0.1 - - [02/Nov/2017:07:17:08 +0800] "GET / HTTP/1.1" 200 8 "-" "curl/7.29.0" "-" ......
定义日志缓存:
Usage example:(官方示例)
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
[root@GaoServer ~]# vim /etc/nginx/nginx.conf ...... access_log /var/log/nginx/access.log main; open_log_file_cache max=100 inactive=20s valid=1m min_uses=2; ...... [root@GaoServer ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@GaoServer ~]# nginx -s reload
相关文章推荐
- 详细解释:nginx中ngx_http_access_module模块(HTTP Access 模块)配置及各个参数含义
- 配置nginx身份验证(ngx_http_auth_basic_module)
- 详细解释:nginx中ngx_http_access_module模块(HTTP Access 模块)配置及各个参数含义
- 详细解释:nginx中ngx_http_auth_basic_module模块(HTTP Auth Basic 模块)配置及各个参数含义 .
- 详细解释:nginx中ngx_http_auth_basic_module模块(HTTP Auth Basic 模块)配置及各个参数含义 .
- nginx静态压缩(ngx_http_gzip_static_module)
- Nginx防攻击工具教程一 ngx_http_limit_conn_module
- nginx服务器access_log日志分析及配置详解
- Nginx模块 ngx_http_limit_conn_module 限制连接数
- 在CentOS 6.9 x86_64的nginx 1.12.2上开启ngx_http_geo_module模块实录
- Nginx Tengine ngx_http_upstream_check_module 健康功能检测使用
- nginx图片过滤处理模块http_image_filter_module安装配置
- nginx外部模块编译安装之ngx_http_accesskey_module防盗链
- nginx 负载均衡之 ngx_http_upstream_hash_module
- Nginx访问日志(access_log)配置及信息详解
- nginx服务器access_log日志分析及配置详解
- nginx限制连接数ngx_http_limit_conn_module模块笔记:二
- nginx handler ngx_http_core_module
- Nginx反向代理Google扩展ngx_http_google_filter_module
- 解读nginx第三方模块ngx_http_auth_request_module