優化Web服務器,實現如下效果:
自定義網站404錯誤頁面
升級nginx至1.15.8版本,開啓status模塊
編寫日誌切割腳本,實現每週五備份日誌
2.2 步驟
實現此案例需要按照如下步驟進行。
步驟一:自定義404錯誤頁面
1)優化前測試(客戶端訪問一個不存在的頁面)。
[root@room9pc01 ~]# firefox http://www.lab.com/git
-
修改Nginx配置文件,自定義錯誤頁面
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
error_page 404 /404.html; //自定義錯誤頁面
[root@web2 ~]# vim /usr/local/nginx/conf/nginx.conf
error_page 404 /404.html; //自定義錯誤頁面
[root@web3 ~]# vim /usr/local/nginx/conf/nginx.conf
error_page 404 /404.html; //自定義錯誤頁面
3) 重啓nginx
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@web2 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@web3 ~]# /usr/local/nginx/sbin/nginx -s reload
步驟二:升級nginx版本,開啓status模塊
1)配置、編譯新的nginx(web1、web2、web3做相同操作,下面以web1爲例)
[root@web1 ~]# tar -xf nginx-1.15.8.tar.gz
[root@web1 ~]# cd nginx-1.15.8
[root@web1 ~]# ./configure \
--with-http_ssl_module \
--with-http_stub_status_module
[root@web1 ~]# make
-
備份老版本nginx,更新新版本nginx
[root@web1 ~]# mv /usr/local/nginx/sbin/nginx{,.bak}
[root@web1 ~]# cp objs/nginx /usr/local/nginx/sbin/
3)修改配置文件
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
... ...
location /status {
stub_status on;
allow 192.168.2.0/24;
deny all;
}
... ...
-
重啓服務
[root@web1 ~]# killall nginx
[root@web1 ~]# /usr/local/nginx/sbin/nginx
步驟三:編寫日誌切割腳本
1)編寫腳本(以web1爲例)
[root@web1 ~]# vim /usr/local/nginx/logbak.sh
#!/bin/bash
date=`date +%Y%m%d`
logpath=/usr/local/nginx/logs
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log $logpath/error-$date.log
kill -USR1 $(cat $logpath/nginx.pid)
2)創建計劃任務
[root@web1 ~]# crontab -e
03 03 * * 5 /usr/local/nginx/logbak.sh