题目要求
服务器RouterSrv上的工作任务
安装 Nginx 组件;
配置文件名为 proxy.conf,放置在/etc/nginx/conf.d/目录下;
为 www.chinaskills.cn 配置代理前端,通过 HTTPS 的访问后端 Web服务器;
后端服务器日志内容需要记录真实客户端的 IP 地址;
缓存后端 Web 服务器上的静态页面;
创建服务监控脚本:/shells/chkWeb.sh;
编写脚本监控公司的网站运行情况;
脚本可以在后台持续运行;
每隔 3S 检查一次网站的运行状态,如果发现异常尝试 3 次;
如果确定网站无法访问,则返回用户“The site is being maintained”。
项目配置:
安装nginx组件:
[root@routesrv ~]# yum install nginx -y
配置nginx代理,并创建缓存目录
[root@routesrv ~]# mkdir /tmp/cache [root@routesrv ~]# vim /etc/nginx/conf.d/proxy.conf
缓存配置:
# 添加 proxy_cache_path /tmp/cache levels=1:2 keys_zone=cskcache:10m;
继续添加代理配置:
location / { proxy_pass https://www.chinaskills.cn/; proxy_ssl_trusted_certificate /root/csk-ca.pem; proxy_cache_key $request_uri; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
后端服务器日志记录真实客户端ip地址:
proxy_set_header X-Real-IP $remote_addr;
整体模块:
[root@routesrv shell]# cat /etc/nginx/conf.d/proxy.conf proxy_cache_path /tmp/cache levels=1:2 keys_zone=cskcache:10m; server { listen 443 ssl; server_name web.chinaskills.cn; ssl_certificate /root/apache.crt; ssl_certificate_key /root/apache.key; root /home; proxy_cache cskcache; proxy_cache_valid 1s; location / { proxy_pass https://www.chinaskills.cn/; proxy_ssl_trusted_certificate /root/csk-ca.pem; proxy_cache_key $request_uri; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } [root@routesrv shell]# [root@routesrv shell]#
重启nginx:
[root@routesrv ~]# systemctl restart nginx
AppSrv服务器修改后端apache日志设置:
[root@appsrv ~]# vim /etc/httpd/conf/httpd.conf # 196行 LogFormat "%h %{x-real-ip}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
说明
%h 是记录访问者的ip,如果在web的前端有一层代理,那么这个%h其实就是代理服务器的ip %{X-Real-IP}i 字段会记录客户端的真实ip,所以将log日志修改为LogFormat "%h %{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 其中%{X-FORWARDED-FOR}i 也是可以的,不过是在后端为nginx的时候使用
重新启动Apache:
[root@appsrv csk-rootca]# systemctl restart httpd
添加测试页,proxy文件结构如下:
[root@routesrv ~]# echo "This site is being maintained" > /home/index.html
创建服务监控脚本:
[root@routersrv ~]# mkdir /shells [root@routersrv ~]# vim /shells/chkweb.sh
添加文档:
[root@routesrv ~]# cat /shell/chkweb.sh #/bin/bash web_server(){ testweb=`curl -k -s -w %{http_code} -o /dev/null https://www.chinaskills.cn` if [[ $testweb -eq 200 ]] then sleep 3s echo "httpd is running" fi } while true do web_server if [[ $testweb -ne 200 ]] then for i in {1..3} do web_server echo Try $i time sleep 3s done echo "httpd is down" sed -i "s/proxy_pass/#&/" /etc/nginx/conf.d/proxy.conf sed -i "s/#root/root/g" /etc/nginx/conf.d/proxy.conf systemctl restart nginx break fi done [root@routesrv ~]#
进行测试启动:
curl -k -s -w %{http_code} -o /dev/null https://www.chinaskills.cn
说明
-k 设置此选项将允许使用无证书的不安全SSL进行连接和传输。 -s 安静模式 -w 操作完成后在返回信息尾部追加指定的内容,例如:%{http_code}、%{local_ip},后面可跟转义字符 \n -o 将返回内容输出到文件
运行测试:
[root@routesrv ~]# bash /shell/chkweb.sh httpd is running httpd is running httpd is running httpd is running httpd is running httpd is running # 将apache关闭之后3秒切换为nginx网页。 Try 1 time Try 2 time Try 3 time httpd is down [root@routesrv ~]#
评分标准
(1) 检查 nginx 服务状态(routersrv 执行 systemctl status nginx) ; 【1 分】
评分要点: 服务状态为 running 即可得分 |
(2) 客户端访问测试(使用 insidecli 访问 web.chinaskills.cn,并且携带在终端输入 hostname 的截图) ; 【2分】
评分要点: 通过 web. chinaskills. cn 能访问到 www. chinaskills. cn 页面的内容, www. chinaskills. cn 是 wordpress 主页面, 而且有绿色带锁的标志, 如果不携带 hostname 的截图或者 hostname 截图主机名不是 insidecli 本题不得分 |
(3) 检查网页日志(appsrv 执行 tail -f /etc/httpd/logs/access_log) ; 【2 分】
评分要点: 能够看到代理服务器 IP 和客户端真实 IP 即可得分, 例如 192. 168. 100. 254 192. 168. 0. 190 的地址。 第二个地址必须是 192. 168. 0. 190, 第一个地址可以是 192. 168. 100. 254 或 192. 168. 0. 254 |
(4) 测试服务器监控脚本(routersrv 先执行网页检测脚本, appsrv 执行 tail -f /etc/httpd/logs/access_log);【3 分】
评分要点: 每三秒作为一次间隔。 进行网页监测, 如图所示的 11: 42: 28 – 11: 42: 31 – 11: 42: 34 |
更多详情免费解析访问——https://blog.51cto.com/lyx888/category3.html