NGINX的反向代理和负载均衡是网站架构中经常用到的一种高并发,高可用的方案,下面我们直接实战操作,当然理论也是要的。
一、反向代理
过程:反向代理:客户端 一>代理 <一> 服务端举个例子: A(客户端)想租一个房子,B(代理)就把这个房子租给了他。 这时候实际上C(服务端)才是房东。 B(代理)是中介把这个房子租给了A(客户端)。这个过程中A(客户端)并不知道这个房子到底谁才是房东 他都有可能认为这个房子就是B(代理)的总结:反向代理中客户端是并不知道自己要访问哪个服务器,反向代理模式屏蔽或者隐藏了真实服务器信息。
结构图

1、什么是正向代理
过程:客户端 <一> 代理 一>服务端举个例子:A(客户端)想租C(服务端)的房子,但是A(客户端)并不认识C(服务端)租不到。 B(代理)认识C(服务端)能租这个房子所以你找了B(代理)帮忙租到了这个房子。总结:正向代理***的特点是客户端非常明确要访问的服务器地址;服务器只清楚请求来自哪个代理服务器,而不清楚来自哪个具体的客户端;正向代理模式屏蔽或者隐藏了真实客户端信息。
2、 正向代理和反向代理的区别

3、反向代理各个参数说明

二、负载均衡
说明:客户端发送的、nginx反向代理服务器接收到的请求数量,就是我们说的负载量,请求数量按照一定的规则进行分发到不同的服务器处理的规则,就是一种均衡规则,所以将服务器接收到的请求按照规则分发的过程,称为负载均衡。
示意图

nginx支持的负载均衡调度算法方式: 关于Nginx负载均衡的6种策略
三、实战操作
1、环境
代理服务器(接收所有请求)
192.168.8.146
服务器
1、192.168.8.228:8080
2、192.168.8.228:8081
3、192.168.8.228:808
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
1)代理服务器

2)服务器
1号服务器
server {
listen 8080;
server_name www.common.com;
root /www/local;
charset utf-8;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2号服务器
server {
listen 8081;
server_name www.common.com;
root /www/test-common/common/public;
charset utf-8;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3号服务器
server {
listen 8082;
server_name www.common.com;
root /www/test;
charset utf-8;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
2、效果



四、总结
这篇文章只是简单的试了下效果,而且均衡的方式只试了权重,包括代理方面,有些参数没有配置,欢迎大家补充尝试。