Configuring Logging 配置日志
NGINX Docs | Configuring Logging https://docs.nginx.com/nginx/admin-guide/monitoring/logging/
【
在上层设置error_log目录,底层设置覆盖高层设置;
】
动手 error_log /data/nginx/log/error/error.log warn; access_log /data/nginx/log/access/nginx-access.log compression;
[root@a /]# mkdir -p /data/nginx/log/{error,access};ssh root@b " mkdir -p /data/nginx/log/{error,access};";ssh root@c "mkdir -p /data/nginx/log/{error,access};";
1 user nginx;
2 worker_processes 8; # = cpu num;
3
4 error_log /data/nginx/log/error/error.log warn; # warn, error crit, alert, and emerg levels are logged. NGINX Docs | Configuring Logging https://docs.nginx.com/ nginx/admin-guide/monitoring/logging/
5
6 #pid logs/nginx.pid;
7
8
9 events {
10 worker_connections 10240;
11 multi_accept on;
12 use epoll;
13 }
14
15
16 http {
17 # log NGINX Docs | Configuring Logging https://docs.nginx.com/nginx/admin-guide/monitoring/logging/
18 log_format compression '$remote_addr - $remote_user [$time_local] '
19 '"$request" $status $body_bytes_sent '
20 '"$http_referer" "$http_user_agent" "$gzip_ratio"';
21 gzip on;
22 access_log /data/nginx/log/access/nginx-access.log compression;
Configuring Logging
This section describes how to configure logging of errors and processed requests in NGINX Open Source and NGINX Plus.
Setting Up the Error Log
NGINX writes information about encountered issues of different severity levels to the error log. The error_log
directive sets up logging to a particular file, stderr
, or syslog
and specifies the minimal severity level of messages to log. By default, the error log is located at logs/error.log (the absolute path depends on the operating system and installation), and messages from all severity levels above the one specified are logged.
The configuration below changes the minimal severity level of error messages to log from error
to warn
:
error_log logs/error.log warn;
In this case, messages of warn
, error
crit
, alert
, and emerg
levels are logged.
The default setting of the error log works globally. To override it, place the error_log
directive in the main (top-level) configuration context. Settings in the main context are always inherited by other configuration levels. The error_log
directive can be also specified at the http
, stream
, server
and location
levels and overrides the setting inherited from the higher levels. In case of an error, the message is written to only one error log, the one closest to the level where the error has occurred. However, if several error_log
directives are specified on the same level, the message are written to all specified logs.
Note: The ability to specify multipleerror_log
directives on the same configuration level was added in open source NGINX version 1.5.2.
Setting Up the Access Log
NGINX writes information about client requests in the access log right after the request is processed. By default, the access log is located at logs/access.log, and the information is written to the log in the predefined combined format. To override the default setting, use the log_format
directive to change the format of logged messages, as well as the access_log
directive to specify the location of the log and its format. The log format is defined using variables.
The following examples define the log format that extends the predefined combined format with the value indicating the ratio of gzip compression of the response. The format is then applied to a virtual server that enables compression.
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;
...
}
}
Another example of the log format enables tracking different time values between NGINX and an upstream server that may help to diagnose a problem if your website experience slowdowns. You can use the following variables to log the indicated time values:
$upstream_connect_time
– The time spent on establishing a connection with an upstream server$upstream_header_time
– The time between establishing a connection and receiving the first byte of the response header from the upstream server$upstream_response_time
– The time between establishing a connection and receiving the last byte of the response body from the upstream server$request_time
– The total time spent processing a request
All time values are measured in seconds with millisecond resolution.
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;
...
}
}
Here are some rules how to read the resulting time values:
- When a request is processed through several servers, the variable contains several values separated by commas
- When there is an internal redirect from one upstream group to another, the values are separated by semicolons
- When a request is unable to reach an upstream server or a full header cannot be received, the variable contains “0” (zero)
- In case of internal error while connecting to an upstream or when a reply is taken from the cache, the variable contains “-” (hyphen)
Logging can be optimized by enabling the buffer for log messages and the cache of descriptors of frequently used log files whose names contain variables. To enable buffering use the buffer
parameter of the access_log
directive to specify the size of the buffer. The buffered messages are then written to the log file when the next log message does not fit into the buffer as well as in some other cases.
To enable caching of log file descriptors, use the open_log_file_cache
directive.
Similar to the error_log
directive, the access_log
directive defined on a particular configuration level overrides the settings from the previous levels. When processing of a request is completed, the message is written to the log that is configured on the current level, or inherited from the previous levels. If one level defines multiple access logs, the message is written to all of them.
Enabling Conditional Logging
Conditional logging allows excluding trivial or unimportant log entries from the access log. In NGINX, conditional logging is enabled by the if
parameter to the access_log
directive.
This example excludes requests with HTTP status codes 2xx
(Success) and 3xx
(Redirection):
map $status $loggable {
~^[23] 0;
default 1;
} access_log /path/to/access.log combined if=$loggable;
Logging to Syslog
The syslog
utility is a standard for computer message logging and allows collecting log messages from different devices on a single syslog server. In NGINX, logging to syslog is configured with the syslog:
prefix in error_log
and access_log
directives.
Syslog messages can be sent to a server=
which can be a domain name, an IP address, or a UNIX-domain socket path. A domain name or IP address can be specified with a port to override the default port, 514. A UNIX-domain socket path can be specified after the unix:
prefix:
error_log server=unix:/var/log/nginx.sock debug;
access_log syslog:server=[2001:db8::1]:1234,facility=local7,tag=nginx,severity=info;
In the example, NGINX error log messages are written to a UNIX domain socket at the debug
logging level, and the access log is written to a syslog server with an IPv6 address and port 1234.
The facility=
parameter specifies the type of program that is logging the message. The default value is local7
. Other possible values are: auth
, authpriv
, daemon
, cron
, ftp
, lpr
, kern
, mail
, news
, syslog
, user
, uucp
, local0 ... local7
.
The tag=
parameter applies a custom tag to syslog messages (nginx
in our example).
The severity=
parameter sets the severity level of syslog messages for access log. Possible values in order of increasing severity are: debug
, info
, notice
, warn
, error
(default), crit
, alert
, and emerg
. Messages are logged at the specified level and all more severe levels. In our example, the severity level error
also enables crit
, alert
, and emerg
levels to be logged.
Live Activity Monitoring
NGINX Plus provides a real-time live activity monitoring interface that shows key load and performance metrics of your HTTP and TCP upstream servers. See the Live Activity Monitoring article for more information.
To learn more about NGINX Plus, please visit the Products page.
Nginx应用-Location路由反向代理及重写策略 请求转发-URL匹配规则 NGINX Reverse Proxy - xl0808tx - 博客园 https://www.cnblogs.com/yuanjiangw/p/9973066.html
参数 说明 示例
$remote_addr 客户端地址 211.28.65.253
$remote_user 客户端用户名称 --
$time_local 访问时间和时区 18
/Jul/2012
:17:00:01 +0800
$request 请求的URI和HTTP协议
"GET /article-10000.html HTTP/1.1"
$http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.wang.com 192.168.100.100
$status HTTP请求状态 200
$upstream_status upstream状态 200
$body_bytes_sent 发送给客户端文件内容大小 1547
$http_referer url跳转来源 https:
//www
.baidu.com/
$http_user_agent 用户终端浏览器等信息 "Mozilla
/4
.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident
/4
.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol SSL协议版本 TLSv1
$ssl_cipher 交换数据中的算法 RC4-SHA
$upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80
$request_time 整个请求的总时间 0.205
$upstream_response_time 请求过程中,upstream响应时间 0.002
Configuring Logging 配置日志的更多相关文章
- Django之logging配置
1. settings.py文件 做开发离不开必定离不开日志, 以下是我在工作中写Django项目常用的logging配置. # 日志配置 BASE_LOG_DIR = os.path.join(BA ...
- Python之配置日志的几种方式(logging模块)
原文:https://blog.csdn.net/WZ18810463869/article/details/81147167 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Py ...
- python项目通过配置文件方式配置日志-logging
背景:项目中引入日志是必须的,这里介绍通过配置文件config.ini的方式配置日志 1.新建config.ini 2.添加配置 [loggers]keys=root,ProxyIP [handler ...
- 转 使用Python的logging.config.fileConfig配置日志
Python的logging.config.fileConfig方式配置日志,通过解析conf配置文件实现.文件 logglogging.conf 配置如下: [loggers]keys=root,f ...
- Confluence 6 配置日志
我们推荐你根据你的需求来配置你自己的 Confluence 日志.你可以有下面 2 种方法来修改你的日志: 通过 Confluence 管理员控制台进行配置 – 你的修改仅在本次修改有效,下次重启后将 ...
- python logging 配置
python logging 配置 在python中,logging由logger,handler,filter,formater四个部分组成,logger是提供我们记录日志的方法:handler是让 ...
- BEA WebLogic Server 10 查看和配置日志
查看和配置日志 WebLogic Server 内的每个子系统都可生成日志消息来传达其状态.例如,当启动 WebLogic Server 实例时,安全子系统会输出消息以报告其初始化状态.为了记录其子系 ...
- python之配置日志的三种方式
以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件,然后使用fileCo ...
- log4j 配置日志输出(log4j.properties)
轉: https://blog.csdn.net/qq_29166327/article/details/80467593 一.入门log4j实例 1.1 下载解压log4j.jar(地址:http: ...
随机推荐
- 插入节点insertBefore()
http://www.imooc.com/code/1699 插入节点insertBefore() insertBefore() 方法可在已有的子节点前插入一个新的子节点. 语法: insertBef ...
- Vue 状态管理
类flux状态管理的官方实现 由于多个状态分散的跨越在许多组件和交互间的各个角落,大型应用复杂度也经常逐渐增长. 为了解决这个问题,vue提供了vuex:我们有收到elm启发的状态管理库,vuex甚至 ...
- asp.net添加用户
AddUser.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <h ...
- phoenix 入门
http://phoenix.apache.org/Phoenix-in-15-minutes-or-less.html Blah, blah, blah - I just want to get s ...
- HeadFirst 设计模式 04 工厂模式
除了 new 操作符之外, 还有更多创造对象的方法. 工厂处理创建对象的细节. 这么做的目的是为了抽象, 例如把创建比萨的代码包装进一个类, 当以后实现改变时, 只需修改这个类即可. 利用静态方法定义 ...
- 数据库 Proc编程一
proc编程 嵌入式sql:sql写入到C语言程序中 proc编程头文件路径 app\xxx\product\\dbhome_1\precomp\public proc编程要注意proc编译器也会使用 ...
- C语言错误 指针的类型错误
//指针的类型错误 #include<stdio.h> #include<stdlib.h> #include<string.h> //用const来限制形参的指向 ...
- heartbeat 心跳技术
转自:http://blog.csdn.net/keda8997110/article/details/8349049 heartbeat 心跳技术原理: heartbeat (Linux-HA)的工 ...
- LINQ to SQL语句(2)Count/Sum/Min/Max/Avg操作符
使用场景 类似于SQL中的聚合函数,用于统计数据,不延迟.如返回序列中的元素数量.求和.最小值.最大值.求平均值. Count 说明:用于返回集合中元素的个数,返回Int类型,生成SQL语句为SELE ...
- galera安装之编译安装xtrabackup 2.2.11
----1.编译安装percona-xtrabackup yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf ...