1、设置gzip的压缩功能
[root@localhost ~]# cp /etc/passwd /usr/local/nginx/html/passwd.html
[root@localhost ~]# ll /usr/local/nginx/html/passwd.html
-rw-r--r-- 1 root root 839 May 11 18:29 /usr/local/nginx/html/passwd.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //看一下gzip块 是否打开
[root@localhost ~]# netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 942/nginx: master p
[root@localhost ~]# ps aux | grep nginx | grep -v "grep"
root 942 0.0 0.1 45948 1116 ? Ss 18:25 0:00 nginx: master process /usr/local/nginx/sbin/nginx
www 943 0.0 0.1 46392 1876 ? S 18:25 0:00 nginx: worker process
- 回车前按下“F12”
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //gzip
[root@localhost ~]# 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
[root@localhost ~]# systemctl restart nginx //高版本系统兼容低版本
- 刷新网页查看大小,304缓存
- 通过查看头部信息也可以看出来文件压缩过了,gzip,deflate
2、设置对图片的expires的缓存功能
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# 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
[root@localhost ~]# nginx -s reload
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html passwd.html
[root@localhost html]# cd -
/root
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.10.3.tar.gz test.png
[root@localhost ~]# mv test.png /usr/local/nginx/html/
[root@localhost ~]# cd -
/usr/local/nginx/html
[root@localhost html]# ls
50x.html index.html passwd.html test.png
- 访问 test.png
- 刷新网页
- 清空记录再刷新 会成 200
3-1、日志切割
[root@localhost ~]# cd /usr/local/nginx/logs //日志文件位置
[root@localhost logs]# ls
access.log error.log nginx.pid
[root@localhost logs]# tail -20 access.log //查看日志
192.168.70.10 - - [11/May/2023:21:43:29 +0800] "GET /favicon.ico HTTP/1.1" 404 572 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36"
192.168.70.10 - - [11/May/2023:21:43:30 +0800] "GET /favicon.ico HTTP/1.1" 404 572 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36"
192.168.70.10 - - [11/May/2023:21:43:31 +0800] "GET /favicon.ico HTTP/1.1"
[root@localhost logs]# tail -10 error.log
2023/05/11 21:43:28 [error] 942#0: *15 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.70.10, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.70.106"
- 创建日志切割脚本
[root@cong11 ~]# cd /usr/local/nginx/logs/
[root@cong11 logs]# vim cut_nginx_log.sh #日志切割脚本
#!/bin/bash
date=$(date +%F -d -1day)
cd /usr/local/nginx/logs
if [ ! -d cut ] ; then
mkdir cut
fi
mv access.log cut/access_$(date +%F -d -1day).log
mv error.log cut/error_$(date +%F -d -1day).log
/usr/local/nginx/sbin/nginx -s reload
tar -jcvf cut/$date.tar.bz2 cut/*
rm -rf cut/access* && rm -rf cut/error*
find -type f -mtime +10 | xargs rm -rf
[root@localhost logs]# ls
access.log cut_nginx_log.sh error.log nginx.pid
[root@localhost logs]# sh cut_nginx_log.sh
cut/2023-05-10.tar.bz2
cut/access_2023-05-10.log
cut/error_2023-05-10.log
[root@localhost logs]# ls
access.log cut cut_nginx_log.sh error.log nginx.pid
[root@localhost logs]# cat access.log
[root@localhost logs]# cat error.log
2023/05/11 22:10:42 [notice] 1334#0: signal process started
[root@localhost logs]# cd cut/
[root@localhost cut]# ls
2023-05-10.tar.bz2
[root@localhost cut]# tar tf 2023-05-10.tar.bz2
cut/2023-05-10.tar.bz2
cut/access_2023-05-10.log
cut/error_2023-05-10.log
[root@cong11 logs]# crontab -els //做计划任务切割
0 0 * * * /bin/sh /usr/local/nginx/logs/cut_nginx_log.sh >/dev/null 2>&1
[root@cong11 logs]# chmod +x cut_nginx_log.sh #添加可执行
4、去掉不需要的日志统计
[root@localhost cut]# cd ..
[root@localhost logs]# cat access.log //刷新一下网页
[root@localhost logs]# cat access.log | tail -1
192.168.70.10 - - [11/May/2023:22:25:27 +0800] "GET /test.png HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36"
[root@cong11 logs]# vim /usr/local/nginx/conf/nginx.conf
location ~ .*\.(js|jpg|jpeg|JPG|JPEG|css|bmp|gif|GIF)$ {
access_log off;
}
[root@localhost logs]# 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
[root@localhost logs]# systemctl restart nginx
[root@localhost logs]# cat access.log | tail -1
192.168.70.10 - - [11/May/2023:22:25:27 +0800] "GET /test.png HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36"
[root@localhost logs]# cat access.log | tail -1 //刷新一下网页
192.168.70.10 - - [11/May/2023:22:25:27 +0800] "GET /test.png HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36"