Nginx日志_log_format
1、Nginx日志类型
(1)error.log-错误日志
(2)access.log-访问日志
(3)log_format日志详解
log_format:
Syntax: | log_format name [escape=default|json|none] string ...; |
Default: | log_format combined "..."; |
Context: | http |
log_format-关键字 name-自定义变量
[escape=default|json|none] string ...-日志具体内容,即HTTP请求变量、nginx内置变量、自定义变量组成
log_format combined "..."-日志默认值
http-只能配置在http作用域下配置
2、Nginx.conf日志配置详解
(1)error_log-关键字,即错误日志
/var/log/nginx/error.log-日志路径
warn-日志级别
(2)log_format-关键字,即日志格式
main-自定义变量标识
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
-日志内容,即HTTP请求变量、nginx内置变量、自定义变量组成
(3)access.log-关键字,即访问日志
access.log-关键字,即访问日志
/var/log/nginx/access.log-日志路径
main-日志格式标识符,即引用的是log_format中自定义日志main变量
3、Nginx变量
(1)HTTP请求变量
- arg_PARAMETER-客户端请求服务端的get请求参数
$arg_PARAMETER #这个变量包含GET请求中,如果有变量PARAMETER时的值。
$args #这个变量等于请求行中(GET请求)的参数,例如foo=123&bar=blahblah;
- http_HEADER-客户端请求服务端的http请求头变量
- sent_http_HEADER-服务端返回给客户端的http响应头变量
(2)内置变量
可以访问nginx官网查看:http://nginx.org/en/docs/http/ngx_http_core_module.html
(3)自定义变量
配置$foo=hello
location /test {
set $foo hello;
echo "foo: $foo";
}
输出
[root@localhost html]# nginx -s reload
[root@localhost html]# curl localhost/test
foo: hello
4、修改nginx.conf-log_format配置
(1)增加HTTP请求变量演示
增加HTTP请求变量演示:arg_PARAMETER、http_HEADER、sent_http_HEADER配置
'"$arg_wd"-"$args"-"$http_x_requested_with"-"$sent_http_cache_control"-"$http_host"-"$sent_http_last_modified" '
(2)重新加载nginx.conf配置
nginx -t -c /etc/nginx/nginx.conf
nginx -s reload -c /etc/nginx/nginx.conf
(3)验证access_log访问日志是否生效
使用curl -v http://localhost:80?wd=wwl&lover=xq测试
进入到/var/log/nginx/access.log目录下访问
验证access_log访问日志格式是否生效: