【Web 集群实战】08_Nginx 访问日志(access_log)

【Web 集群实战】08_Nginx 访问日志(access_log)

标签(空格分隔): Web集群实战


1. Nginx 访问日志介绍

  • Nginx 软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由 ngx_http_log_module 模块负责。对应的官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html

2. 访问日志参数

  • 控制日志的参数
log_format用来定义记录日志的格式
access_log用来指定日志文件的路径及使用何种日志格式记录日志
  • Nginx 日志变量
$remote_addr客户端地址
$remote_user用于HTTP基础认证服务的用户名
$request代表客户端的请求地址
$statusHTTP响应代码
$time_local服务器时间(LOG Format 格式)
$body_bytes_sent传输给客户端的字节数,响应头不计算在内;这个变量和 Apache 的 mod_log_config 模块中的"%B"参数保持兼容
$http_refererurl 跳转来源,用来记录从那个页面链接访问过来的
$http_user_agent用户终端浏览器等信息

参考链接:https://www.cnblogs.com/wajika/p/6426270.html

3. 访问日志配置

  • nginx.conf 如下:
worker_processes 1;
error_log   logs/error.log;
events {
    worker_connections 1024;
}
http {
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalived_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"';
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/statu.conf;
}
  • 在每个虚拟主机里进行配置,记录用户访问日志。
[root@ylt001 conf]# cat extra/www.conf
#www virtualhost by ylt
server {
    listen       80;
    server_name  www.yangyangyang.org yangyangyang.org;
    location / {
        root   html/www;
        index  index.html index.htm;
    }
    access_log logs/access_www.log ;
}
  • 检查语法,重新加载配置
[root@ylt001 conf]# ../sbin/nginx
[root@ylt001 conf]# ../sbin/nginx -s reload
  • 用浏览器模拟用户访问生成日志,在服务器上查看日志结果
[root@ylt001 conf]# tail -1 /etc/hosts
192.168.2.132 www.yangyangyang.org 
[root@ylt001 conf]# curl www.yangyangyang.org
www.yangyangyang.org
[root@ylt001 conf]# ll ../logs/access_www.log
-rw-r--r-- 1 root root 3595 Sep 19 14:47 ../logs/access_www.log
[root@ylt001 conf]# tail -1 ../logs/access_www.log
192.168.2.132 - - [10/Oct/2018:22:03:29 +0800] "GET / HTTP/1.1" 200 36 "-" "curl/7.29.0"
  • 使用谷歌浏览器模拟用户访问生成日志
[root@ylt001 conf]# tail -1 ../logs/access_www.log
192.168.2.1 - - [10/Oct/2018:22:05:58 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"

4. Nginx 访问日志轮询切割

  • 按天切割的脚本文件如下:
[root@ylt001 conf]# cat /server/script/cut_nginx_log.sh
#!/bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

注:脚本实现切割 Nginx 日志的思想为将正在写入的 Nginx 日志(access_www.log)改名为带日期的格式文件(20180919_access_www.log),然后平滑重新加载 Nginx,生成新的 Nginx 日志(access_www.log)。

  • 通过定时任务实现每天 00 点整定时执行 /server/script/cut_nginx_log.sh 切割日志
[root@ylt001 logs]# cat >>/var/spool/cron/root <<EOF
>#cut nginx access log by ylt001
>00 00 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1
>EOF
  • 最终的日志切割效果如下
[root@ylt001 ~]# /bin/sh /server/script/cut_nginx_log.sh
[root@ylt001 ~]# cd /application/nginx/logs
[root@ylt001 logs]# ll
total 56
-rw-r--r-- 1 root root  4417 Sep 19 15:58 20180919_access_www.log
-rw-r--r-- 1 root root   105 Sep 19 15:30 access_bbs.log
-rw-r--r-- 1 root root   189 Sep 19 14:46 access_blog.log
-rw-r--r-- 1 root root 15520 Sep 18 21:38 access.log
-rw-r--r-- 1 root root     0 Sep 19 16:26 access_www.log
-rw-r--r-- 1 root root 17912 Sep 19 16:26 error.log
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值