1.nginx的限流控制
(1)浏览器sticky控制
unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
ls
cd nginx-1.21.6/
./configure --help
该指令的作用是在编译过程中添加一个已有模块
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
make
cd objs/
ls
\cp -f nginx /usr/local/nginx/sbin/
nginx -s stop
nginx
netstat -antlp
cd /usr/local/nginx/conf/
ls
vim nginx.conf
编辑内容
nginx -s reload
测试:
在浏览器中输入192.168.2.77
可以得出,当不切换浏览器时,无论怎样刷新,得到的都是server3主机的界面,只有当server3主机关闭或者切换浏览器时,才会得到server2的界面
(2) nginx高并发限流
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
curl 192.168.2.77/index.html
由上图可知,可以正常访问
ab -n 10 -c 10 http://192.168.2.77/index.html
由上图发现,10次均正常访问,无失败信息
cd /usr/local/nginx/html/
mkdir download
cd
ls
cp westos.png /usr/local/nginx/html/download
cd /usr/local/nginx/html/download
ls
ab -n 10 -c 10 http://192.168.2.77/download/westos.png
以上命令的作用是测试高并发限制,发现并无错误信息
(3)单位时间访问请求的限流
vim /usr/local/nginx/conf/nginx.conf
编辑内容
该指令的作用是限制每秒只处理一个任务
nginx -s reload
ab -n 10 -c 1 http://192.168.2.77/download/westos.png
发现十个任务中失败了九个,成功了一个,符合配置文件中的限制
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
ab -n 10 -c 1 http://192.168.2.77/download/westos.png
由上图可知,虽然十个任务均完成,但是用时为9秒,符合限制条件
(4)单位时间的下载流量限制
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
cd /usr/local/nginx/html/download
ls
du -sh
由上图可知,该图片的大小为276K
ab -n 3 -c 1 http://192.168.2.77/download/westos.png
从图中可以看出,完成三次任务耗时6秒,由于单位时间限制流量为100K,单个任务的大小为276K,估算完成所有任务耗时约为6.5s左右,与图中相符
2.nginx的配置管理
(1)自动索引
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
测试:
(2) 缓存
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
curl -I 192.168.2.77/download/westos.png
(3) 日志轮询
vim /opt/nginxlog.sh
编辑内容
cd /usr/local/nginx/logs
ls
chmod +x /opt/nginxlog.sh
/opt/nginxlog.sh
ls
crontab -e
编辑内容
(4)goaccess日志可视化
tar zxf goaccess-1.4.tar.gz
ls
cd goaccess-1.4/
./configure --enable-utf8 --enable-geoip=legacy
cd
yum install GeoIP-devel-1.5.0-13.el7.x86_64.rpm
cd goaccess-1.4/
./configure --enable-utf8 --enable-geoip=legacy
yum install ncurses-devel -y
./configure --enable-utf8 --enable-geoip=legacy
make
make install
由上图可知,goaccess软件已安装成功
cd
goaccess /usr/local/nginx/logs/access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html &
测试:
(5)禁用日志
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
测试:
(6)站点目录和文件的限制
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
测试:
curl localhost/status
由上图可知,只有本机可以监控,其余主机无法监控
3.nginx的重定向
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
curl 192.168.2.77
由上图知,服务器返回值为500
vim /usr/local/nginx/conf/nginx.conf
编辑内容
nginx -s reload
curl 192.168.2.77
由上图可得,返回值位301,被重定向