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 ~]# 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