系统服务(22年国赛)—— Web Proxy服务(web代理)

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”

关闭防火墙和SELinux

systemctl stop firewalld
setenforce 0

安装Ngninx

 使用WinScp软件或Windows自带SCP命令上传Centos的扩展yum源软件包到RouterSrv上
Test
 修改原先的本地yum源文件

vim /etc/yum.repos.d/local.repo
在原先的基础上添加以下内容:
[package]
name=local
baseurl=file:///mnt/package
gpgcheck=0
enable=1
保存退出
yum update                            # 更新yum源软件列表
yum install nginx  -y                 # 使用yum安装nginx
systemctl restart nginx               # 启动nginx

 打开浏览器,输入nginx服务器的ip地址如果出现以下内容则安装成功!
Test

申请SSL签字证书

 RouterSrv

mkdir /CA && cd /CA
openssl genrsa -out nginx.key 2048
openssl req -new -key nginx.key  -out nginx.csr -days 365
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [XX]:CN
    State or Province Name (full name) []:China
    Locality Name (eg, city) [Default City]:BeiJing
    Organization Name (eg, company) [Default Company Ltd]:skills
    Organizational Unit Name (eg, section) []:Operations Departments 
    Common Name (eg, your name or your server's hostname) []:web.chinaskills.cn
    Email Address []:
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:

 将生成的待签字复制到AppSrv上

scp /CA/nginx.csr root@192.168.100.100:/csk-rootca

 AppSrv上进行签字后送回

cd /csk-rootca/
openssl ca -in nginx.csr -out certs/nginx.crt
scp certs/nginx.crt root@192.168.100.254:/CA

创建Nginx的代理配置文件和配置缓存

vim /etc/nginx/conf.d/proxy.conf
添加以下内容:
server {
        listen 80;
        listen 443 ssl;
        ssl_certificate "/CA/nginx.crt";
        ssl_certificate_key "/CA/nginx.key";
        server_name web.chinaskills.cn;
        location ~.*\.* {
        proxy_pass http://www.chinaskills.cn;
        proxy_set_header x-real-ip $remote_addr;

        proxy_cache proxy;
        proxy_set_header Host $host;
        proxy_cache_valid 200 301 302 5m;

}
}
保存退出

 缓存配置

vim /etc/nginx/nginx.conf
在http下添加以下内容:
    proxy_cache_path /cache levels=1:2 keys_zone=proxy:20m max_size=20m;
保存退出
mkdir /cache                                        # 创建缓存目录

 在AppSrv上将Apache的缓存日志修改

vim /etc/httpd/conf/httpd.conf 
在%h后添加%{x-real-ip}i即可
     LogFormat "%h %{x-real-ip}i %l %u %t \"%r\" %>s %b \"%{Referer}i
\" \"%{User-Agent}i\"" combined
保存退出
systemctl restart httpd                              # 重启Apache服务

创建服务监控脚本:/shells/chkWeb.sh

 RouterSrv

mkdir /shells 
vim /shells/chkWeb.sh
添加以下内容:
#!/bin/bash
url=https://www.chinaskills.cn
o=`curl -s -k $url -I |grep 'OK' |awk '{print $2}'`
while [ true ]; do
/bin/sleep 3
  if [[ $o -eq 200 ]];then
     echo "$url 正常打开 3s"
     else
        for i in 1 2 3;do
          if [[ $o -eq 200 ]];then
              echo "$url 正常打开"
          else echo "$url 异常 $i"
          fi
        done
        echo "The site is being maintained"
  fi
done
保存退出

 重启nginx服务

systemctl restart nginx

测试(已做好DNS解析):

 InsideCli
 刚开始Nginx的缓存目录里面是什么都没有的

cd /cache
ll

Test
接下来我们在客户端上访问一下
 客户端打开火狐浏览器,输入:https://web.chinaskills.cn
Test
此时再查看缓存目录(nginx.conf中指定的)会发现多了个目录
Test
  !!如果出现配置的www站点的反向代理但是跳转到的是Apache的download站点
 原因:
 1、www和download站点都占用的是443端口;
 2、配置文件加载的时候download比www站点更先加载,因为我的两个配置文件名分别是www.chinaskills.cn.conf和download.chinaskills.cn.conf,而在英文排序中d在w前面,所以会被先加载(亲自测试过)
 解决方法:
 1、修改download站点的端口;
 2、在http.conf文件的倒数第二行添加引用www站点配置文件的配置,保证它在download配置文件之前被引用(推荐),如下所示:
Test
  在Apache服务器的日志文件中查看访问者的真实IP地址

tail -n 1 /etc/httpd/logs/access_log 
    192.168.100.254 192.168.0.190 - - [26/Mar/2024:19:43:22 +0800] "GET / HTTP/1.0" 200 51962 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"

  运行监控脚本进行测试:

bash /shells/chkWeb.sh
显示以下内容:
    https://www.chinaskills.cn 正常打开 3s
    https://www.chinaskills.cn 正常打开 3s
    https://www.chinaskills.cn 正常打开 3s

  关闭Apache服务后再次尝试

systemctl stop httpd
bash /shells/chkWeb.sh
显示以下内容:
    https://www.chinaskills.cn 异常 1
    https://www.chinaskills.cn 异常 2
    https://www.chinaskills.cn 异常 3
    网站正在维护中...
无标签
打赏
评论区
头像
文章目录