目录
作为代理服务器是当客户端访问代理服务器时,代理服务器代理客户端去访问真实web服务器。proxy_pass; 用来设置将客户端请求转发给的后端服务器的主机。
需要模块ngx_http_upstream_module支持。
单台反向代理
在第三台主机上下载安装httpd,在主页面/var/www/html/下写一个页面index.html
并启动systemctl start httpd别忘了关防火墙

在7-1上做代理服务器,写入location配置文件:

当有客户端访问我的IP时,我把请求转给指定的IP处理。保存退出,重新加载配置文件nginx -s reload
使用7-2访问7-1那么应该显示Node3的网页内容,也就是显示7-3 7-3

proxy_pass后面加不加/的问题:
location /test {
proxy_pass http://192.168.114.30; #不加/
}
这里直接是将/test附加到192.168.114.30后面,也就是192.168.114.30/test/index.html,也就是主站点要有test这个文件夹。
在web网站建立主页面index.html内容为/var/www/html在主页面中建立test文件夹,里面创建一个内容为/var/www/html/test的文件index.html

使用Node2访问192.168.114.10/test
可以发现获取到的是/var/www/html/test也就是192.168.114.10/test/index.html类似于root

如果加/:
location /test {
proxy_pass http://192.168.114.30/;
}
发现实际返回的是proxy_pass后面的url的内容,也就是192.168.114.10/index.html类似于alias

反向代理动静分离
动静分离是实现客户端访问动态和静态资源时,代理服务器通过匹配url访问不同的服务器。
用浏览器作为客户端,7-1反向代理,7-2作为静态服务器,7-3作为动态服务器。
7-2和7-3使用httpd即可,

7-1上的配置:

重启配置文件:nginx -s reload
开启正则表达式~*,不能加/
当在浏览器访问192.168.114.10/a.jpg时,代理服务器会匹配以.jpg结尾的url,转给Node3主机处理,访问a.jpg图片:

当在浏览器访问192.168.114.10/api时,匹配访问192.168.114.20静态服务器。

反向代理负载均衡
当客户端访问网页时,代理服务器通过对客户端发出的请求分别交给真实服务端依次提供服务,解决单台服务器负载问题。
当7-4客户机访问代理服务器7-1时,7-1分别交给7-2或7-3依次执行,对多个服务端设置成一个服务器组,upstream,在location模块中写入代理转交给组名称。
7-1代理服务器端的配置:

在web服务器组中,还可以加权重,weight=2;
server 192.168.114.20 weight=2;默认都是1,加权重之后就是192.168.114.20大概是两次,192.168.114.10大概是1次,并不是绝对的。
我们在两台真实服务器端使用httpd模拟web服务器。分别在主页写入文件:


在7-4上测试如下,发现每次访问代理服务器,7-2和7-3都会交替提供服务。实现负载均衡效果。

-------end---------