[关闭]
@yanglt7 2018-10-21T15:54:04.000000Z 字数 3013 阅读 690

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

Web集群实战


1. Nginx 访问日志介绍

2. 访问日志参数

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

- Nginx 日志变量

$remote_addr 客户端地址
$remote_user 用于HTTP基础认证服务的用户名
$request 代表客户端的请求地址
$status HTTP响应代码
$time_local 服务器时间(LOG Format 格式)
$body_bytes_sent 传输给客户端的字节数,响应头不计算在内;这个变量和 Apache 的 mod_log_config 模块中的"%B"参数保持兼容
$http_referer url 跳转来源,用来记录从那个页面链接访问过来的
$http_user_agent 用户终端浏览器等信息

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

3. 访问日志配置

  1. [root@ylt001 conf]# sed -n '21,23 s/#//gp' nginx.conf.default
  2. log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  10. '$status $body_bytes_sent "$http_referer" '
  11. '"$http_user_agent" "$http_x_forwarded_for"';
  12. sendfile on;
  13. keepalived_timeout 65;
  14. include extra/www.conf;
  15. include extra/bbs.conf;
  16. include extra/blog.conf;
  17. include extra/statu.conf;
  18. }
  1. [root@ylt001 conf]# cat extra/www.conf
  2. #www virtualhost by ylt
  3. server {
  4. listen 80;
  5. server_name www.yangyangyang.org yangyangyang.org;
  6. location / {
  7. root html/www;
  8. index index.html index.htm;
  9. }
  10. }
  1. [root@ylt001 conf]# ../sbin/nginx
  2. [root@ylt001 conf]# ../sbin/nginx -s reload
  1. [root@ylt001 conf]# curl www.yangyangyang.org
  2. www.yangyangyang.org
  3. [root@ylt001 conf]# ll ../logs/access_www.log
  4. -rw-r--r-- 1 root root 3595 Sep 19 14:47 ../logs/access_www.log
  5. [root@ylt001 conf]# tail -1 ../logs/access_www.log
  1. [root@ylt001 conf]# tail -1 ../logs/access_www.log

4. Nginx 访问日志轮询切割

  1. [root@ylt001 conf]# cat /server/script/cut_nginx_log.sh
  2. #!/bin/sh
  3. Dateformat=`date +%Y%m%d`
  4. Basedir="/application/nginx"
  5. Nginxlogdir="$Basedir/logs"
  6. Logname="access_www"
  7. [ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
  8. [ -f ${Logname}.log ] || exit 1
  9. /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
  10. $Basedir/sbin/nginx -s reload

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

  1. [root@ylt001 logs]# cat >>/var/spool/cron/root <<EOF
  2. >#cut nginx access log by ylt001
  3. >00 00 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1
  4. >EOF
  1. [root@ylt001 ~]# cd /application/nginx/logs
  2. [root@ylt001 logs]# ll
  3. [root@ylt001 ~]# /bin/sh /server/script/cut_nginx_log.sh
  4. [root@ylt001 ~]# cd /application/nginx/logs
  5. [root@ylt001 logs]# ll
  6. total 56
  7. -rw-r--r-- 1 root root 4417 Sep 19 15:58 20180919_access_www.log
  8. -rw-r--r-- 1 root root 105 Sep 19 15:30 access_bbs.log
  9. -rw-r--r-- 1 root root 189 Sep 19 14:46 access_blog.log
  10. -rw-r--r-- 1 root root 15520 Sep 18 21:38 access.log
  11. -rw-r--r-- 1 root root 0 Sep 19 16:26 access_www.log
  12. -rw-r--r-- 1 root root 17912 Sep 19 16:26 error.log
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注