
正常情况下,客户端去访问代理服务器,然后代理服务器再取访问真实服务器,在真实服务器上,只能显示代理服务器的ip地址,而不显示客户端的ip地址,如果想让客户端的ip地址也能在真实服务端看见,这一技术叫做IP透传
$http_x_forwarded_for:这个变量我们要赋值给它,并且会一直保留,每次都会追加
可以在主配置文件里面的日志文件里面看见这一选项


#proxy_set_header X-Real-IP $remote_addr; #只添加客户端IP到请求报文头部,转发至后端服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP和反向代理服务器IP到请求报文头部
如果有一个代理服务器,可以写第一个
如果有多个代理服务器,则下面的
如果既想看见客户端ip又想看见代理服务器 就添加第二行
在真实服务端的日制中查看,是看不见客户端或者代理服务器端的ip的

如果想看见真实访问的ip地址,就是客户端跟代理服务端的ip时 看以下操作
单机IP透传
在代理服务端(7-1) (nginx系统)
在子配置文件里面配置内容 X-Forwarded-For 这是一个变量
7-2真实服务器上(httpd系统)
用set list 可以查看时在196行(set nu可以看到多少行),然后进行添加 (只需要将次变量加入即可)
X-Forwarded-For


然后重启

然后再用另外一台客户端(7-3)去curl192.168.68.3的代理服务器

此时 再去7-2查看日志


多机IP透传 (把所有机器换成nginx,都是nginx系统)
在每个代理服务器上面都要设置:proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP和反向代理服务器IP到请求报文头部

在nginx中,变量 X-Forwarded-For已经写好了,都写在日志文件中了 只需要在每个代理服务端的配置文件中写入proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for即可

代理服务器1:192.168.68.3
代理服务器2:192.168.68.4
真实服务器:192.168.68.5
(访问代理服务器1 就跳转到代理服务器2 访问代理服务2 就跳转到真是服务器)
代理服务器1配置:

代理服务器二配置:

随后,重载

真实服务器 (7-3)
首先关闭防火墙
其次yum 安装nginx
yum install -y epel-release
yum install nginx -y
然后开启服务器nginx

yum安装的nginx的主页面 在/usr/share/nginx/html中

然后在代理服务器curl 192.168.68.3自己

最后,在真是服务器上面查看日志,会出现代理ip 跟客户端ip

http反向代理负载均衡 upstream


upstream 在http模块里面
调度算法:

代理服务器:192.168.68.3
真实服务器1:192.168.68.4
真是服务器2::192.168.68.5
代理服务器 :
修改主配置文件
加入服务器组

然后将子配置文件注释掉,如果不注释,不能进行下面的location操作

随后,在location里面修改

随后保存并退出

真实服务器1:

真实服务器2

随后,在代理服务器服务器上curl
一会7-2 一会7-3

以上为轮询算法(nginx默认),一人一次的意思
nginx有检测机制,如果有一台服务器挂掉,nginx只会去访问正常的那一台 (健康性检查)
举例说明:
当7-3 服务器挂了,nginx只能去访问7-2


然后再去代理服务器上面curl

验证成功 ,只能去健康的服务器访问

去关闭的那台真实服务器上查看
此时只有I个包,正常情况下,tcp三次握手最少应该有6个包 所以这边失败

加权轮询 weight

weight:5 意思为上面分5次 下面没写,默认一次
实例:
在代理服务器(192.168.68.3)上的主配置文件里面修改

随后,保存重载 然后在curl
可以看出192.168.68.4 出现3次 192.168.68.5出现一次


#server支持的parameters如下:
weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number #给当前后端server设置最大活动链接数,默认为0表示没有限制
max_fails=number #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性检查,而非周期性的探测
fail_timeout=time #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒
backup #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器 sorry server 自己不能转自己
down #标记为down状态
resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx
max_fails 表示链接次数,当检验计算机是否挂掉,max_fails=3 就表示尝试tcp连接次计算机三次 ,如果不行就认为挂掉
fail_timeout :如果检测到计算机还活着,等个多少秒再上线
backup:备份服务器,当前面几台服务器都挂掉的时候,可以使用备份服务器
演示备份服务器流程:
在第一台服务器7-1 主配置文件里面修改

第二步在备份服务器上面下载nginx



模拟192.168.68.4 跟192.168.68.5 损坏 就是将其nginx关掉

随后,再次进入到7-1 curl自己 会出现备份服务器上的网页

hash算法
hash的作用:保持会话一致
什么是会话?

ip hash 根据ip地址,然后得出一个值 这样就会永久保持会话。第一次访问1服务器 就永久访问1服务器
url hash 根据url 然后得出一个值 这样就会永久保持会话
ip hash实例:在7-1 代理服务上操作

因为第一次被curl到7-2上,所以一辈子都在7-2上

hash有个致命缺点,就是跟权重相关 例如权重是2,则hash/2 hash/(除以)总权重,所以会使用路径hash (url hash)
url hash
hash $requst_uri
在7-1 代理服务器主配置文件里面修改

然后再通过服务器7-1 去curl

cookie hash 缓存hash

案例根据cookic里面的关键字去查找
在7-1 代理服务器上主配置文件里面修改
然后重启

随后去curl

fair
根据响应时间来分配,响应时间短的优先分配