在生产环境中每一天的日志文件都是要打包备份的,如果每天都手动的去截取日志,重命名这样就很不方便,所以我们编写一个脚本并建立一个定时任务来进行这些工作
1.编辑脚本文件,加权限
[root@server1 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@server1 nginx]# cd logs/
[root@server1 logs]# ls
access.log error.log
[root@server1 logs]# vim backup.sh
[root@server1 logs]# ls
access.log backup.sh error.log
[root@server1 logs]#
脚本内容
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/oldlogs
CUR_LOG_PATH=/usr/local/nginx/logs
YESTERDAY=$(date +%F -d -1day)
mv $CUR_LOG_PATH/access.log $LOG_PATH/${YESTERDAY}_access.log
mv $CUR_LOG_PATH/error.log $LOG_PATH/${YESTERDAY}_error.log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
给脚本加权限
[root@server1 logs]# ls
access.log backup.sh error.log
[root@server1 logs]# chmod +x backup.sh
[root@server1 logs]# ll
2.创建目录
3.执行脚本,查看目录
[root@server1 logs]# ./backup.sh
[root@server1 logs]# cd oldlogs/
[root@server1 logs]# crontab -e
写入下面的内容:
0 0 * * * /bin/bash /usr/local/nginx/logs/backup.sh
1.准备测试页
[root@server1 ~]# cd /usr/local/nginx/
[root@server1 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html index.html
[root@server1 html]# vim index.html
为了方便我把/etc/passwd下的内容导入默认发布文件中,复制多次
2.浏览器上测试发布内容
重启服务
systemctl restart nginx ##做了脚本启动可以使用该命令
/usr/local/nginx/sbin/nginx -s reload ##通用
按F12调出开发者工具
可以看见网页大小与默认发布文件大小基本一致,为133.19K
3.网页压缩配置——修改配置文件
[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
修改内容如下
33 gzip on;
34 gzip_min_length 1;
35 gzip_comp_level 2;
36 gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd/php image/jpeg image/gif image/png;
[root@server1 ~]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ./nginx -s reload
4.再次打开网页查看
1.设置访问速率,一秒一个请求,所以请求十次10秒左右(限制客户端请求的并发量为1个)
(1)编辑配置文件
[root@server1 system]# cd
[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]#
37 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
51 location /search/ {
52 limit_req zone=one;
53 }
[root@server1 conf]# cd ../html
[root@server1 html]# ls
50x.html index.html
语法检测没有问题
[root@server1 conf]# ../sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
(2)创建search目录
[root@server1 html]# mkdir search
[root@server1 html]# cd search/
[root@server1 search]# ls
(3)下载一个图片
[root@server1 search]# systemctl reload nginx ##重启服务
(4)打开浏览器可以看到
(5)物理机测试
[root@foundation19 ~]# ab -c 1 -n 10 http://172.25.6.1/search/hello.jpg
2.burst
(1)编辑配置文件,添加brust=5
[root@server1 conf]# vim nginx.conf
[root@server1 html]# systemctl reload nginx.service ##重新加载服务
测试:
[root@foundation19 ~]# ab -c 1 -n 5 http://172.25.6.1/search/hello.jpg
等待大概5秒后
3.设置每秒访问50k,访问5次,116*5 = 580 ,580/50约10秒
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# systemctl reload nginx
[root@server1 conf]# du -sh /usr/local/nginx/html/search/hello.jpg
116K /usr/local/nginx/html/search/vim.jpg
[root@server1 conf]#
测试:
[root@foundation19 ~]# ab -c 1 -n 5 http://172.25.6.1/search/hello.jpg
4.内置变量,1秒1k
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# systemctl reload nginx
测试:打开浏览器访问,一直转圈
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# systemctl reload nginx
[root@server1 conf]#
[root@server1 conf]# cd ../logs/
[root@server1 logs]# ls
access.log backup.sh error.log nginx.pid oldlogs westos.access.log
[root@server1 logs]#
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 '$status $body_bytes_sent "$http_referer" '
23 '"$http_user_agent" "$http_x_forwarded_for"';
37 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
38 server {
39 listen 80;
40 server_name localhost;
41
42 #charset koi8-r;
43
44 access_log logs/westos.access.log main;
45
46 location / {
47 root html;
48 #set $limit_rate 1k;
49 index index.html index.htm;
50 }
51
52 location /search/ {
53 #limit_rate 50k;
54 limit_req zone=one burst=5;
55 }
测试:
[root@foundation19 ~]# ab -c 1 -n 10 http://172.25.6.1/search/hello.jpg
在自定义的日志文件中查看