第10周作业
1 使用logrotate+定时任务对nginx的访问日志进行轮询切割。
注:写出logrotate的配置与定时任务的内容。
/application/nginx/logs/access_bbs.log /application/nginx/logs/access_www.log /application/nginx/logs/access_blog.log /application/nginx/logs/access.log{dailyrotate 7missingoknotifemptydateextcompressdelaycompresscreate 600 www wwwsharedscriptspostrotate if [ -f /application/nginx/logs/nginx.pid]; then kill -USR1 `cat /application/nginx/logs/nginx.pid` fiendscript}[root@web01 logs]# crontab -e#logrotate ngix access logs at 0:00 everyday0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
2 说出nginx日志中下面参数的含义:log_format; access_log; root;index;sever_name;listen;keepalive_timeout;
log_format; 日志的格式access_log; 访问日志root; 网站站点目录index; index.html文件路径sever_name; 站点(虚拟主机)名称listen; 监听端口keepalive_timeout; 连接活跃时间
3 配置三个网站 bbs.lidao.com blog.lidao.com
要求:
-
网站的站点目录在/app下面 并以www bbs blog命名
-
访问日志存放在/app/log下面并且要求每个网站的日志不同
-
对访问日志进行每日的定时切割并删除7天之前的日志
给出操作过程及配置文件和脚本内容。
创建相关目录mkdir /app/{www,bbs,blog} -pmkdir /app/log –p1 网站的站点目录[root@web01 conf]# tree /app//app/├── bbs├── blog└── www2 网站的conf[root@web01 conf]# tree extra/extra/├── bbs.conf├── blog.conf└── www.conf3 主配置 nginx.conf[root@web01 conf]# vim nginx.confworker_processes 1;events { worker_connections 1024;}http { 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"'; include extra/*; # 当前路径下的文件夹}4 站点配置内容[root@web01 extra]# cat bbs.conf blog.conf www.confserver { listen 80; server_name bbs.lidao.com; access_log /app/log/access_bbs.log main; location / { root /app/bbs; index index.html index.htm; } }server { listen 80; server_name blog.lidao.com; access_log /app/log/access_blog.log main; location / { root /app/blog; index index.html index.htm; } }server { listen 80; server_name www.lidao.com lidao.com; access_log /app/log/access_www.log main; location / { root /app/www; index index.html index.htm; } }5 创建index.html文件内容[root@web01 extra]# for name in www bbs blog; do echo "$name.lidao.com" > /app/$name/index.html; done[root@web01 extra]# tree /app//app/├── bbs│ └── index.html├── blog│ └── index.html├── log└── www └── index.html6 查看index.html文件内容[root@web01 extra]# for name in www bbs blog; do cat /app/$name/index.html ; donewww.lidao.combbs.lidao.comblog.lidao.com7 测试[root@web01 extra]# curl www.lidao.comwww.lidao.com[root@web01 log]# cat access_www.log10.0.0.8 - - [22/Aug/2018:06:32:43 +0800] "GET / HTTP/1.1" 200 14 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"10.0.0.1 - - [22/Aug/2018:06:33:05 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"10.0.0.1 - - [22/Aug/2018:06:33:05 +0800] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"10.0.0.1 - - [22/Aug/2018:06:33:48 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" "-"8 书写日志轮训切割脚本[root@web01 scripts]# vim rotate_log.sh#!/bin/shfor name in www bbs blog;do /bin/mv /app/log/access_${name}.log /app/log/$(date +%Y%m%d)_access_${name}.logdone;find /app/log -type f -name "*.log" -mtime +1 |xargs rm –i /app/log/{}/application/nginx/sbin/nginx -s reload9 测试和查看结果[root@web01 scripts]# sh /server/scripts/rotate_log.sh[root@web01 log]# ll /app/log/total 0-rw-r--r--. 1 root root 0 Aug 24 20:43 20180824_access_bbs.log-rw-r--r--. 1 root root 0 Aug 24 20:43 20180824_access_blog.log-rw-r--r--. 1 root root 0 Aug 24 20:43 20180824_access_www.log-rw-r--r--. 1 root root 0 Aug 24 20:44 access_bbs.log-rw-r--r--. 1 root root 0 Aug 24 20:44 access_blog.log-rw-r--r--. 1 root root 0 Aug 24 20:44 access_www.log10 书写定时任务[root@web01 scripts]# crontab -e# cut nginx log00 0 * * * /bin/sh /server/scripts/rotate_log.sh >/dev/null 2>&1
4 给出3个nginx部署和使用过程中的错误及解决方法
1 配置日志格式后检测报错[root@web01 conf]# nginx -tnginx: [emerg] unknown log format "basic" in /application/nginx-1.14.0//conf/extra/bbs.conf:4nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test failed经过几次检查才发现是,日志格式format位于配置文件conf后面导致,加载时找不到定义的日志名而报错。错误配置worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/*; log_format basic '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';}解决:正确配置顺序 log_format basic '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include extra/*;2 缺少相应模块 error: SSL modules require the OpenSSL library.解决:yum install openssl-devel -y3 日志切割 logrotate postrotate if [ -f /application/nginx/logs/nginx.pid]; then kill -USR1 `cat /application/nginx/logs/nginx.pid` fi endscript 之间的nginx.pid 文件应填实际的文件的路径