网站首页 文章专栏 nginx日志配置(log_format)
nginx日志配置,经常用在nginx.conf配置文件中,用于自己定义nginx成功日志的格式
eg:
log_format main '$remote_addr "-" $remote_user [$time_local] "$request" "$request_body" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$upstream_addr" $upstream_response_time $request_time'; log_format main_cookie '$remote_addr "-" $remote_user [$time_local] "$request" "$request_body" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$upstream_addr" $upstream_response_time $request_time $cookie_uid'; log_format json '{"host":"$host","remote_addr":"$remote_addr","http_x_forwarded_for":"$http_x_forwarded_for","time_local":"$time_iso8601",' '"request_uri":"$request_uri","method":"$request_method","status":"$status",' '"request_body":"$request_body",' '"body_bytes_sent":"$body_bytes_sent","http_referer":"$http_referer",' '"http_user_agent":"$http_user_agent",' '"upstream_addr":"$upstream_addr","upstream_response_time":"$upstream_response_time","request_time":"$request_time"}'; log_format json_cookie '{"host":"$host","remote_addr":"$remote_addr","http_x_forwarded_for":"$http_x_forwarded_for","time_local":"$time_iso8601",' '"request_uri":"$request_uri","method":"$request_method","status":"$status",' '"request_body":"$request_body",' '"body_bytes_sent":"$body_bytes_sent","http_referer":"$http_referer",' '"http_user_agent":"$http_user_agent",' '"upstream_addr":"$upstream_addr","upstream_response_time":"$upstream_response_time","request_time":"$request_time","cookie_uid":"$cookie_uid"}';
main的日志效果为:
180.110.143.116 "-" - [10/Oct/2019:15:34:18 +0800] "GET /studydetail/8/69.html HTTP/1.1" "-" 200 20278 "http://www.hebinghua.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" "-" "127.0.0.1:9000" 0.051 0.051
json_cookie的效果为:
{"host":"admin.hebinghua.com","remote_addr":"180.110.143.116","http_x_forwarded_for":"-","time_local":"2019-10-10T15:31:46+08:00","request_uri":"/study/linuxlist.html","method":"GET","status":"200","request_body":"-","body_bytes_sent":"39380","http_referer":"http://admin.hebinghua.com/study/editlinux/id/25.html?bakurl=http://admin.hebinghua.com/study/linuxlist.html","http_user_agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36","upstream_addr":"127.0.0.1:9000","upstream_response_time":"0.066","request_time":"0.066","cookie_uid":"-"}
常用的nginx变量
$args此变量与请求行中的参数相等
$content_length等于请求行的“Content_Length”的值。
$content_type等同与请求头部的”Content_Type”的值
$document_root等同于当前请求的root指令指定的值
$document_uri与$uri一样
$host与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样
$limit_rate允许限制的连接速率
$request_method等同于request的method,通常是“GET”或“POST”
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_port客户端port
$remote_user等同于用户名,由ngx_http_auth_basic_module认证
$request_filename当前请求的文件的路径名,由root或alias和URI request组合而成
$request_body_file
$request_uri含有参数的完整的初始URI
$query_string与$args一样
$sheeme http模式(http,https)尽在要求是评估例如
Rewrite ^(.+)$ $sheme://example.com$; Redirect;
$server_protocol等同于request的协议,使用“HTTP/或“HTTP/
$server_addr request到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。
$server_name请求到达的服务器名
$server_port请求到达的服务器的端口号
$time_local: 用来记录访问时间与时区
$uri等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index
$status: 用来记录请求状态;成功是200
$body_bytes_sent :记录发送给客户端文件主体内容大小
$http_referer:用来记录从那个页面链接访问过来的
$http_user_agent:记录客户浏览器的相关信息
转载请注明出处