Nginx访问日志(access_log)配置及信息详解

Nginx访问日志(access_log)配置及信息详解

通过访问日志,可以知晓用户的地址,网站的哪些部分最受欢迎,用户的浏览时间,对大多数用户用的的浏览器做出针对性优化。

Nginx访问日志(access_log)介绍

    Nginx会把每个用户访问往咱的日志信息记录到指定的日志文件里,供网站管理员分析用户浏览行为等,此功能又 ngx_http_log_module 模块负责。

访问日志参数

    Nginx访问日志主要有两个参数控制

log_format  #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)

access_log  #用来指定日至文件的路径及使用的何种日志格式记录日志

lof_format的默认值:

#    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的默认值:

#access_log  logs/access.log  main;

log_format语法格式及参数语法说明如下:

    log_format    <NAME>    <Strin­­­g>;

    关键字         格式标签   日志格式

 

    关键字:其中关键字error_log不能改变

    格式标签:格式标签是给一套日志格式设置一个独特的名字

    日志格式:给日志设置格式

 

log_format格式变量:

    $remote_addr  #记录访问网站的客户端地址

    $remote_user  #远程客户端用户名

    $time_local  #记录访问时间与时区

    $request  #用户的http请求起始行信息

    $status  #http状态码,记录请求返回的状态码,例如:200301404

    $body_bytes_sent  #服务器发送给客户端的响应body字节数

    $http_referer  #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。

    $http_user_agent  #记录客户端访问信息,例如:浏览器、手机客户端等

    $http_x_forwarded_for  #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置

access_log语法格式及参数语法说明如下:

    access_log    <FILE>    <NAME>;

    关键字         日志文件   格式标签

 

    关键字:其中关键字error_log不能改变

    日志文件:可以指定任意存放日志的目录

    格式标签:给日志文件套用指定的日志格式

 

其他语法:

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

    access_log path [format [buffer=size [flush=time]] [if=condition]];

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

    access_log syslog:server=address[,parameter=value] [format [if=condition]];

    说明:

    buffer=size  #为存放访问日志的缓冲区大小

    flush=time  #为缓冲区的日志刷到磁盘的时间

    gzip[=level]  #表示压缩级别

    [if = condition]  #表示其他条件

一般场景这些参数都无需配置,极端优化才有可能会考虑这些参数。

lof_format参数的标签段位置:

http

access_log参数的标签段位置:

http, server, location, if in location, limit_except

参考资料:http://nginx.org/en/docs/http/ngx_http_log_module.html

Nginx配置访问日志过程介绍

(1)创建log_format语句

vi conf/nginx.conf

#vi编辑nginx主配置文件,添加标签为mainlog_format格式(http标签内,在所有的server标签内可以调用)

文件内容:

worker_processes  1;

error_log logs/error.log error;

events {

    worker_connections  1024;

}

http {

    include status.conf;

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    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;

    server {

        listen       80;

        server_name  localhost;

                rewrite ^/.* http://www.abc.com permanent;

    }

    include vhost/*.conf;

}

(2)插入access_log语句

vi conf/vhost/www.abc.com.conf

#vi编辑虚拟主机配置文件

文件内容:

server {

        access_log /data/log/www;

        listen 80;

        server_name abc.com www.abc.com;

        location / {

                root /data/www/www;

                index index.html index.htm;

        }

        error_log    logs/error_www.abc.com.log    error;

        access_log    logs/access_www.abc.com.log    main;

        #新增内容↑

}

(3)重启服务

确认无误便可重启,操作如下:

nginx -t

#结果显示oksuccess没问题便可重启

nginx -s reload

(4)查看访问日志文件

ll logs/access_www.abc.com.log

-rw-r--r-- 1 root root 2305 Jun 13 18:25 logs/access_www.abc.com.log

查看是否生产该文件,生成该文件则配置成功。

转载于:https://www.cnblogs.com/czlun/articles/7010591.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值