配置nginx日志

配置 Nginx

先来看一个实际的配置文件:

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr|||$remote_user|||$time_local|||$request_uri'   
                      '|||$status|||$body_bytes_sent|||$http_referer'   
                      '|||$http_user_agent|||$http_x_forwarded_for';   
  
    access_log  off;   
    #access_log  logs/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;
 
        access_log  /usr/local/nginx/nginxlog/access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

nginx 日志相关指令主要有两条,
log_format,用来设置日志格式,
access_log,用来指定日志文件的存放路径、格式和缓存大小

1、log_format 格式

    log_format       name( 格式名字)  格式样式(即想要得到什么样的日志内容)


默认的示例:
  log_format     main               '$remote_addr - $remote_user [$time_local] "$request" '
                                              '$status $body_bytes_s ent "$http_referer" '
                                              '"$http_user_agent" "$http_x_forwarded_for"'
注释:
$remote_addr   与$http_x_forwarded_for 用以记录客户端的ip地址;
$remote_user   :用来记录客户端用户名称;
$time_local  : 用来记录访问时间与时区; 
$request  :  用来记录请求的url与http协议;

$request_uri: /stat.php?id=1585378&web_id=1585378
$uri /stat.php
$document_uri: /stat.php
$status     :  用来记录请求状态;成功是200,
$body_bytes_s ent  :记录发送给客户端文件主体内容大小;
$http_referer  :用来记录从那个页面链接访问过来的;
$http_user_agent  :记录客户毒啊浏览器的相关信息;


通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址;

    log_format     mylogformat     ' $http_x_forwarded_for- $remote_user [$time_local] '
                                                  ' "$request"   '$status $body_bytes_s ent '
                                                  ' "$http_referer" "$http_user_agent" ';


2、用access_log指令日志文件存放路径;
用了log_format 指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;
access_log        path(存放路径)                   format (自定义日志名称)
示例:
#access_log             logs/access.log              main;
我们用log_format 定义了一个mylogformat的日志 我们可以写成这样
access_log             logs/access.log         mylogformat ;


如果不想启用日志 :
access_log off ;


在定义日志目录中要注意的是,

        1、nginx进程设置的用户和组必须有对该路径 创建文件的权限,假设nginx的usr指令设置的用户名 和用户组都是www,而logs 目录的用户名和组是root,那么日志文件将无法被创建;

        2、一般情况下,如果nginx.conf配置文件里的http里面没设置access_log off;默认是会把所有访问日志输出到/var/log/nginx目录下面,现在要把不同网站的日志区分开来,所以添加了access_log off。

        按照之前很多网上的配置方法是在各个虚拟主机配置文件的server段里添加日志格式和日志输出路径,如下

log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log  /home/wwwlogs/ddhow.com.log access;
error_log  /home/wwwlogs/ddhow.com-error.log;

lua记录日志:

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format tick "$msec|||$u_t|||$remote_addr|||$u_domain|||$u_url|||$u_title|||$u_referrer|||$u_sh|||$u_sw|||$u_cd|||$u_lang|||$http_user_agent|||$u_utrace|||$u_account|||$u_time";
    

    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

	location /1.gif {
    	#伪装成gif文件
    	default_type image/gif;
    	#本身关闭access_log,通过subrequest记录log
    	access_log off;
    	access_by_lua "
    	-- 用户跟踪cookie名为__utrace
    	local uid = ngx.var.cookie___utrace
    	if not uid then
    	-- 如果没有则生成一个跟踪cookie,算法为md5(时间戳+IP+客户端信息)
    	uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent)
    	end
    	ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'}
    	if ngx.var.arg_domain then
    	-- 通过subrequest到/i-log记录日志,将参数和用户跟踪cookie带过去
    	ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid .. '&time=' .. ngx.localtime())
    	end
    	";
    	#此请求不缓存
    	add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT";
    	add_header Pragma "no-cache";
    	add_header Cache-Control "no-cache, max-age=0, must-revalidate";
    	#返回一个1×1的空gif图片
    	empty_gif;
    	}
    	location /i-log {
    	#内部location,不允许外部直接访问
    	internal;
   	 #设置变量,注意需要unescape
    	set_unescape_uri $u_domain $arg_domain;
	set_unescape_uri $u_t $arg_t;
    	set_unescape_uri $u_url $arg_url;
    	set_unescape_uri $u_title $arg_title;
    	set_unescape_uri $u_referrer $arg_referrer;
    	set_unescape_uri $u_sh $arg_sh;
    	set_unescape_uri $u_sw $arg_sw;
    	set_unescape_uri $u_cd $arg_cd;
    	set_unescape_uri $u_lang $arg_lang;
    	set_unescape_uri $u_utrace $arg_utrace;
    	set_unescape_uri $u_account $arg_account;
	set_unescape_uri $u_time $arg_time;
    	#打开日志
    	log_subrequest on;
    	#记录日志到ma.log,实际应用中最好加buffer,格式为tick
    	access_log /usr/local/nginx/nginxlog/access.log tick;
    	#输出空字符串
    	echo '';
    	}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
	
	# remove the robots line if you want to use wordpress' virtual robots.txt
       	location = /robots.txt  { access_log off; log_not_found off; }
        location = /favicon.ico { access_log off; log_not_found off; }
        # this prevents hidden files (beginning with a period) from being served
        location ~ /\.          { access_log off; log_not_found off; deny all; }

    }



}

相关推荐