Real-combat Nginx (2)-Log module

Source: Internet
Author: User

Nginx has a very flexible logging mode. Each level of configuration can have its own independent access logs. The log format is defined by the Log_format command. The ngx_http_log_module is used to define the request log format.


I. Detailed logging configuration

1. Access_log Instructions

Syntax: access_log path [format [buffer=size [Flush=time]]];access_log path format gzip[=level] [buffer=size] [flush=time]; Access_log Syslog:server=address[,parameter=value] [format];access_log off;
Default value: Access_log logs/access.log combined; config segment: http, server, location, if in location, limit_except

gzip compression level.

Buffer sets the memory buffer size.

The maximum amount of time that flush is saved in the cache.

Log not logged: Access_log off;

Logging using the default combined format: Access_log logs/access.log or access_log logs/access.log main;


2. Log_format Instructions

Syntax: Log_format name string ...; Default value: Log_format main "..."; Configuration segment: http


Name represents the format name, and string represents the format of the literal. Log_format has a default main log format that does not have to be set, which is equivalent to the Apache combined log format, as follows:

Log_format Main ' $remote _addr-$remote _user [$time _local] "" $request "$status $body _bytes_sent "$http _referer" "$http _user_agent";



The log format allows for the inclusion of variable annotations as follows:

$remote _addr $http _x_forwarded_for #记录客户端IP地址 $remote _user #记录客户端用户名称 $request #记录请求的URL和HTTP协议 $status #记录请求状态 $body _ Bytes_sent #发送给客户端的字节数, excluding the size of the response header, which is compatible with the "%B" parameter in the Apache module MOD_LOG_CONFIG. $bytes _sent #发送给客户端的总字节数. $connection #连接的序列号. $connection _requests #当前通过一个连接获得的请求数量. $msec #日志写入时间. The unit is seconds, and the precision is milliseconds. $pipe #如果请求是通过HTTP流水线 (pipelined), the pipe value is "P", otherwise ".". $http _referer #记录从哪个页面链接访问过来的 $http _user_agent #记录客户端浏览器相关信息 $request _length #请求的长度 (including request lines, request headers, and request bodies). $request _time #请求处理时间, in seconds, precision milliseconds, starting with the first byte read into the client until the last word is character to the client for log writes. $time _iso8601 #ISO8601标准格式下的本地时间. $time _local #通用日志格式下的本地时间.


Two. Configure the log format of the HTTP segment for Apache-style logs, which are called in the virtual host;

    1. Edit the master configuration file to configure the default log format for the HTTP segment:

[Email protected] ~]# vim/etc/nginx/nginx.conflog_format main ' $remote _addr-$remote _user [$time _local] ' "$request" $status $body _bytes_sent "" $http _referer "" $http _user_agent ";


2. Call in the virtual host;

[Email protected] ~]# vim/etc/nginx/extra/nginx-vhost.conf server {Listen 80;        server_name www.stu31.com;        Index index.html index.htm index.php;        ROOT/WWW/VHOSTS/WWW1; Access_log/var/log/nginx/www.stu31.com.log main;}


3. Grammar check:

[Email protected] ~]# service nginx configtestnginx:the configuration file/etc/nginx/nginx.conf syntax is oknginx:conf Iguration file/etc/nginx/nginx.conf Test is successful


4. Restart the Nginx service:

[Email protected] ~]# service nginx restartnginx:the configuration file/etc/nginx/nginx.conf syntax is Oknginx:configu Ration file/etc/nginx/nginx.conf test is successfulstopping nginx: [OK]sta rting nginx: [OK]


5. Connection test:

[[email protected] ~]# elinks-dump http://172.16.31.40 172.16.31.40 www.stu31.com[[email protected] ~]# elinks-dump HT Tp://www.stu31.com 172.16.31.40 www.stu31.com


6. View the log:

[[email protected] ~]# tail /var/log/nginx/www.stu31.com.log 172.16.31.254 -  - [27/dec/2014:10:37:56 +0800] get / http/1.1304 0 - mozilla/5.0   (WINDOWS NT 6.1; WOW64)  AppleWebKit/537.36  (Khtml, like gecko)   CHROME/37.0.2062.124 SAFARI/537.36 -127.0.0.1 - - [27/DEC/2014:10:40:17 +0800]  GET / HTTP/1.1200 27 - ELinks/0.12pre5  (textmode; linux; -)  -127.0.0.1 - - [27/dec/2014:10:40:29 +0800] get / http/1.1200 27  - ELinks/0.12pre5  (textmode; linux; -)  -172.16.31.254 - -   [27/dec/2014:11:38:48 +0800]    "get / http/1.1"   304  0    "-"    "mozilla/5.0  (WINDOWS NT 6.1; WOW64)  applewebkit/537.36   (khtml, like gEcko)  chrome/37.0.2062.124 safari/537.36 " 172.16.31.254 - -  [27/dec/ 2014:11:38:50 +0800]    "get / http/1.1"   304  0   " -"  " mozilla/5.0  (windows nt 6.1; wow64)  AppleWebKit/537.36  (khtml,  like gecko)  chrome/37.0.2062.124 safari/537.36 " 172.16.31.254 - -   [27/Dec/2014:11:38:50 +0800]    "get / http/1.1"   304   0   "-"    "mozilla/5.0  (WINDOWS NT 6.1; WOW64)  applewebkit/537.36   (Khtml, like gecko)  chrome/37.0.2062.124 safari/537.36 " 172.16.31.254 -  -  [27/Dec/2014:11:38:52 +0800]    "get / http/1.1"    304  0   "-"    "mozilla/5.0  (WINDOWS NT 6.1; WOW64)   applewebkit/537.36  (KHTML,&NBSp;like gecko)  chrome/37.0.2062.124 safari/537.36 " 172.16.31.254 - -   [27/dec/2014:11:38:58 +0800]    "get / http/1.1"   200  27    "-"    "mozilla/5.0  (WINDOWS NT 6.1; WOW64)  applewebkit/537.36   (Khtml, like gecko)  chrome/37.0.2062.124 safari/537.36 " 172.16.31.40 -  -  [27/Dec/2014:11:39:54 +0800]    "get / http/1.1"    200  27   "-"    "elinks/0.12pre5  (textmode; linux; -)"   127.0.0.1 - -  [27/dec/2014:11:40:00 +0800]    "GET / HTTP/ 1.1 "  200  27  "-"  " elinks/0.12pre5  (textmode; linux;  -)"







This article is from "Dragon Guardian" blog, please make sure to keep this source http://sohudrgon.blog.51cto.com/3088108/1596657

Real-combat Nginx (2)-Log module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.