Nginx实战(五)访问日志配置详解

================= 

      Nginx访问日志介绍

=================

       Nginx软件会把每个用户访问网站的日志信息记录到日志文件中,供网站提供者分析用户行为等,功能由ngx_http_log_module模块提供,默认已指定的格式写入请求日志。

 =================

         配置访问日志

=================

1.查看默认的格式.

[root@test nginx]# sed -n '21,25 s/#//gp' conf/nginx.conf.default 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main; #nginx记录日志的默认配置参数。

#main是日志格式指定的标签,记录日志通过main标签选择指定格式即可。

  访问日志的两个参数的语法

<log_format>     <flag>      <string...>; 标签段位置 HTTP。

<access_log>     <path>     <[format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]>;

access_log      off;  #关闭access_log,即不记录访问日志。

标签段位置:http, server, location, if in location, limit_except

   log_format 参数解释

$remote_addr #记录访问网站的客户端地址。
$http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的x_firwarded_for设置。
$remote_user #远程客户端用户名称。
$time_local #记录访问时间与时区。
$request #用户的http请求起始行的信息。
$status #http状态码,记录请求返回的状态。 200、404、301等。
$body_bytes_sents #服务器发送给客户端的响应body字节数。
$http_referer #记录此次请求是从哪个连接访问过来的。
$http_user_agent #记录客户端访问信息,例如浏览器、手机客户端。

2.将这段配置写入 nginx.conf 的http区块中

[root@test nginx]# cat conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    include       extra/*.conf;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

以一台虚拟主机为例子,配置一个访问日志。

server {
            access_log logs/www_access.log main;
            error_log logs/google_error.log error;
            listen       80;
            server_name  www.google.com www.alias.com;
            location / {
            root   html/www;
            index  index.html index.htm;
            }
            location /basic_status {
                 stub_status on;
                 access_log off;
                 allow 10.0.0.0/24;
                 deny all;
             }

}

3.重启Nginx服务,并检查日志是否生成。

[root@test nginx]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@test nginx]# /application/nginx/sbin/nginx -s reload
[root@test nginx]# ls logs/www_access.log 
logs/www_access.log

#显示OK即为成功。

4.进行访问测试

[root@test nginx]# curl www.google.com
i am google
[root@test ~]# tail -f /application/nginx/logs/www_access.log 
10.0.0.7 - - [14/May/2018:13:34:37 +0800] "GET / HTTP/1.1" 200 12 "-" "curl/7.29.0" "-"
# ‘-’代表无,日志定义的格式可以自行设置。



猜你喜欢

转载自blog.csdn.net/csdn_changsha/article/details/80308018
今日推荐