# 反向代理

不持支反向代理 https 服务器因为它是要对应证书

image_2023-01-28-16-06-35

  • lvs 模型

image-20230129073941158

# 网关,代理与反向代理

image_2023-01-28-16-06-45

# 反向代理在系统架构中的应用场景

image_2023-01-28-16-40-11

# 负载均衡器

  • 需要被负载均衡的服务器我们称之为服务器的集群

image_2023-01-28-16-51-19

# Nginx 的反向代理配置

  • 关键字: proxy_pass
    • 在 location 中配置而且与 root 二选一不能同时使用
server {
#  listen       80;
  listen       8080;
  server_name  localhost;
  location / {
      proxy_pass http://www.qq.com;
      #root   html;
      index  index.html index.htm;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   html;
  }
}

# proxy_pass 跳转外网网站

  • 当我们访问时就会访问到 qq 的官网里

image_2023-01-29-09-46-14

  • 但我们将反向代理的域名写成 qq.com
server {                      
    listen       8080;
    #server_name 192.168.244.128;
    #server_name localhost;
    #在win中浏览器输入Linux的IP也能访问到项目,但不能win中输入bogon,Linux的主机名这样访问不到
    server_name bogon;
    location / {
        #反向代理关键字:proxy_pass
        proxy_pass http://qq.com;
        #root 只能查找本机中根目录的项目
        #root   http://www.qq.com;
        index  index.html index.htm;
    }                         
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }           
}

重启服务器后访问并查看网络情况

image_2023-01-29-09-47-45

可以看到 IP 域名请求时进行了重定向跳转到了 www.qq.com 页面中,我们 302 重定向会发送两次请求

image_2023-01-29-09-49-51

# proxy_pass 跳转本地其它服务器中

  • 跟 win 与 Linux 的原理一样,我们访问另一台服务器需要配置其 Linux 的 IP 地址

  • 访问到另一台服务器后另一台服务器访问自己本机中的项目

server {                      
    listen       8080;
    server_name bogon;        

    location / {
        #反向代理关键字:proxy_pass
        proxy_pass http://192.169.244.102;
        #root 只能查找本机中根目录的项目
        #root   http://www.qq.com;
        index  index.html index.htm;
    }                         
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }           
}

另一个服务器的配置文件

server {                      
    listen       8080;
    server_name bogon;        

    location / {
        #反向代理关键字:proxy_pass
        proxy_pass http://www.qq.com;
        #root 只能查找本机中根目录的项目
        #root   http://www.qq.com;
        index  index.html index.htm;
    }                         
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }           
}
  1. 配置这两个 Linux 虚拟机让其保证可以访问到自己创建的 index 页面如下效果:
  • 虚拟机 1 号

image_2023-01-29-14-25-12

  • 虚拟机 2 号

image_2023-01-29-14-25-48

  1. 在一个主的 nginx 虚拟机的 nginx 配置文件中配置
  • 虚拟机 0 号
server {             
   listen       8080;
   server_name localhost;
   
   location / {     
       #反向代理关键字:proxy_pass http://后面为虚拟机1号的ip与端口号8080
       proxy_pass http://192.168.244.138:8080;
       #root 只能查找本机中根目录的项目
       #root   http://www.qq.com;
       index  index.html index.htm;
   }                
   error_page   500 502 503 504  /50x.html;                                
   location = /50x.html {                                                  
       root   html; 
   }                
}  
  • 虚拟机 1 号
server {          
   listen       8080;
   server_name  localhost;
                 
   location / {
       root   /www;
       index  index.html index.htm;
   }             
                 
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   html;
   }             
                      
}

当使用虚拟机 0 号的 ip 来访问浏览器请求时会反向代理到虚拟机 1 号中的根目录的项目中,效果如下

image_2023-01-29-14-32-52

# 基于反向代理的负载均衡 (轮询):[一人一下]

  1. 安装至少三台 Linux 如果是虚拟机可以进行多开多安装
  • 在 0 号虚拟机的配置文件中配置如下:

在 proxy_pass 后定义 http:// 别名,这个别名对应着 upstream 而 upstream 是和 server 同级的,在 upstream 中定义多个 (一组) 服务器

#别名的定义,定义一组服务器
    #upstream和server是同一级别的
    upstream httpds{
        server 192.168.244.138:8080;
        server 192.168.244.139:8080;
    }

    server {
        listen       8080;
        server_name localhost;
        location / {
            #httpds随便起的别名,但是要与upstream对应得上,upstream和server是同一级别的
            proxy_pass http://httpds;
            #root 只能查找本机中根目录的项目
            #root   http://www.qq.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

浏览器请求效果如下:

image_2023-01-29-16-09-07

再次访问请求效果如下:

image_2023-01-29-16-10-10

# 负载均衡策略

weight: 设置群众值 (权重)

通过群众分配比例,每台服务器多少请求转发过去

image_2023-01-29-17-10-29

  • 0 号 Linux 的配置如下:
#别名的定义,定义一组服务器
    #upstream和server是同一级别的
    upstream httpds{
        server 192.168.244.138:8080 weight=8;
        server 192.168.244.139:8080 weight=2;                                                                                                             
    }

    server {
        listen       8080;
        server_name localhost;
        location / {
            #httpds随便起的别名,但是要与upstream对应得上,upstream和server是同一级别的
            proxy_pass http://httpds;
            #root 只能查找本机中根目录的项目
            #root   http://www.qq.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

1

1 号 Linux 项目的请求转发几率比 2 号 Linux 大很多

down: 设置后不参与负载均衡器

  • 在 weight 后面设置
#别名的定义,定义一组服务器
    #upstream和server是同一级别的
    upstream httpds{
        server 192.168.244.138:8080 weight=8 down;
        server 192.168.244.139:8080 weight=2;
    }
                                                                                                                    
    server {
        listen       8080;
        server_name localhost;
        location / {
            #httpds随便起的别名,但是要与upstream对应得上,upstream和server是同一级别的
            proxy_pass http://httpds;
            #root 只能查找本机中根目录的项目
            #root   http://www.qq.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

效果如下:

  • 无论怎么刷新都只会反向代理请求 2 号的项目,1 号已经不参与了

image_2023-01-29-16-59-00

backup: 备用

没有任何可用的群众时使用,正常情况下不参与

0 号 Linux 配置如下:

#别名的定义,定义一组服务器
    #upstream和server是同一级别的
    upstream httpds{
        server 192.168.244.138:8080 weight=8 down;
        server 192.168.244.139:8080 weight=2 backup;                                                                                                      
    }

    server {
        listen       8080;

        server_name localhost;
        location / {
                #httpds随便起的别名,但是要与upstream对应得上,upstream和server是同一级别的
                proxy_pass http://httpds;
                #root 只能查找本机中根目录的项目
            #root   http://www.qq.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

效果如下:

image_2023-01-29-17-03-46

ip_hash

根据客户端的 ip 地址转发同一台服务器,可以保持会话

least_conn

最少链接访问

url_hash

根据用户访问的 url 定向转发请求

fair

根据后端服务器响应时间转发请求

image_2023-01-29-19-00-03

# 反向代理 Tomcat

操作步骤:

  1. 开启至少两台 Linux

0 号 Linux 的配置如下:

#别名的定义,定义一组服务器
    #upstream和server是同一级别的
    upstream httpds{
        server 192.168.244.138:8080 weight=8 down;
        server 192.168.244.139:8088 weight=2 backup;
    }

    server {
        listen       8080;

        server_name localhost;
        location / {
                #httpds随便起的别名,但是要与upstream对应得上,upstream和server是同一级别的
                proxy_pass http://httpds;
                #root 只能查找本机中根目录的项目                                                                                                          
            #root   http://www.qq.com;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

使用 upstream 负载均衡了两台 Linux 不过我们只使用 到了一个将 138 的 Linux 设置了 down 为不使用因为 1 号 Linux 的 nginx 端口号和 tomcat 的端口号有冲突问题所以 1 号 Linux 的 nginx 端口号我该为了 8088

  • 如下是 1 号 Linux 的配置:
server {
        listen       8088;
        server_name  localhost;
                                                                                                                                                          
        location / {
            #这里配置的是访问Tomcat的ip地址
            proxy_pass http://192.168.244.139:8080;
            #root /www;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}

浏览器请求效果如下:我们发送 0 号虚拟机的请求反向代理到我们的 1 号 Linux 中的 Tomcat 上

image_2023-01-30-08-26-10