1.什么是代理
生活中代理的场景:
房屋中介
订票网站
2.正向代理与反向代理
image.png
以访问GOO为例,客户端连接到VPN相当于正向代理,VPN代理请求访问后端服务器并返回属于反向代理
3.Nginx支持的代理协议
image.png
Nginx反向代理模式
image.png
4.Nginx反向代理参数解释
# 将配置文件写入新文件,调用的时候include引用即可
[root@lb01 ~]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host; # lb服务器将用户访问网站HOST信息传递给后端的WEB服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 将用户的真实IP传递给后端WEB服务器(主要是日志文件显示)
proxy_connect_timeout 30; #代理与后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 60; #后端服务器数据回传给nginx代理超时时间
proxy_read_timeout 60; #代理等待后端服务器的响应时间
proxy_buffering on; #把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传,不是全部接收完再传给客户端
proxy_buffer_size 32k; #设置nginx代理保存用户头信息的缓冲区大小
proxy_buffers 4 128k; #proxy_buffers缓冲区
5.Nginx简单反向代理实战
需求访问lb01的80端口代理到web01的8080端口
- web01配置
cat > /etc/nginx/conf.d/web.conf<<EOF
server {
listen 8080;
server_name www.test.com;
location / {
root /code;
index index.html;
}
}
EOF
mkdir /code -p
echo "web01" >/code/index.html
nginx -t
systemctl restart nginx
- lb01配置
cat > /etc/nginx/conf.d/proxy.conf<<EOF
server {
listen 80;
server_name www.test.com; #和需要代理的WEB01服务器域名保持一致
location / {
proxy_pass http://172.16.1.51:8080; #需要代理的web01服务器IP和端口
include proxy_params; #反向代理的配置文件引用
}
}
EOF
nginx -t
systemctl restart nginx
-
访问测试修改windows的host文件或在lb服务器上host配置
image.png
# ib 服务配置hosts解析,让域名解析lb自身IP
[root@lb01 /etc/nginx/conf.d]# echo '10.0.0.5 www.test.com' >> /etc/hosts
[root@lb01 /etc/nginx/conf.d]# curl www.test.com
web01
6.Nginx负载均衡
- 为什么需要负载均衡
我们web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷
我们使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器中实现负载的分发
那么会大大提升系统的吞吐率、请求性能、高容灾
- 负载均衡和反向代理的关系
因为反向代理多台机器,所有可以达到负载均衡的效果
image.png
负载均衡算法
image.png
负载均衡配置参数
image.png
负载均衡实战1
需求,访问www.test.com网站,平均的方式到达服务器web01和web02
服务器要求:一台lb服务器,2台nginx
- lb服务器配置
# bbs_pools为地址池名称
upstream bbs_pools{
server 172.16.1.51:8080;
server 172.16.1.52:8080;
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://bbs_pools; 访问域名解析后去bbs_pools地址池查找相应的服务器
include proxy_params;
}
}
nginx -t
systemctl restart nginx
- web服务器配置(两台一样。代码目录下index.html文件为自身主机名)
[root@db01 /etc/nginx/conf.d]# cat web.conf
server {
listen 8080;
server_name www.test.com;
location / {
root /code;
index index.html;
}
}
echo "$(hostname)" > /code/index.html #两台WEB分别写入
systemctl restart nginx
-
测试
image.png
负载均衡实战2
访问bbs.test.com 跳转到172.16.1.52
访问www.test.com 跳转到172.16.1.51
- LB配置文件
[root@lb01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/proxy.conf
upstream www_pools{
server 172.16.1.51:8080;
}
upstream bbs_pools{
server 172.16.1.52:8080;
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://www_pools;
include proxy_params;
}
}
server {
listen 80;
server_name bbs.test.com;
location / {
proxy_pass http://bbs_pools;
include proxy_params;
}
}
nginx -t
systemctl restart nginx
- web01服务配置
[root@db01 ~]# cat /etc/nginx/conf.d/web.conf
server {
listen 8080;
server_name www.test.com;
location / {
root /code;
index index.html;
}
}
[root@db01 ~]# cat /code/index.html
www
nginx -t
systemctl restart nginx
- web02服务配置
[root@web02 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/bbs.conf
server {
listen 8080;
server_name bbs.test.com;
location / {
root /code;
index index.html;
}
}
[root@web02 /etc/nginx/conf.d]# cat /code/index.html
bbs
nginx -t
systemctl restart nginx
-
访问测试
image.png
image.png
负载均衡实战3
一台服务存在多个域名,访问不同的域名跳转到不同对应页面
此实验可以验证LB服务只负责转发,url请求能否拿到结果还是取决于后端的WEB集群配置
- LB服务器配置
[root@lb01 /etc/nginx/conf.d]# cat proxy.conf
upstream www_pools{
server 172.16.1.51:8080;
server 172.16.1.52:8080;
}
server {
listen 80;
server_name www.test.com bbs.test.com;
location / {
proxy_pass http://www_pools;
include proxy_params;
}
}
WEB服务器配置(两台必须保持配置一致)
[root@db01 /code]# cat /etc/nginx/conf.d/bbs.conf
server {
listen 8080;
server_name bbs.test.com;
location / {
root /code;
index bbs.html;
}
}
[root@db01 /code]# cat /etc/nginx/conf.d/www.conf
server {
listen 8080;
server_name www.test.com;
location / {
root /code;
index www.html;
}
}
#两台机都需执行测试命令
echo "$(hostname) bbs" > /code/bbs.html
echo "$(hostname) www" > /code/www.html
- 访问测试
#LB服务配置hosts解析
[root@lb01 /etc/nginx/conf.d]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.5 www.test.com bbs.test.com
[root@lb01 /etc/nginx/conf.d]# curl www.test.com
web01 www
[root@lb01 /etc/nginx/conf.d]# curl www.test.com
web02 www
[root@lb01 /etc/nginx/conf.d]# curl bbs.test.com
web01 bbs
[root@lb01 /etc/nginx/conf.d]# curl bbs.test.com
web02 bbs
7.负载均衡调度算法实验
- weight(加权)
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf
upstream www_pools{
server 172.16.1.51:8080 weight=1; #这里1和2不是指访问的次数,而是指比例
server 172.16.1.52:8080 weight=2;
}
server {
listen 80;
server_name www.test.com bbs.test.com;
location / {
proxy_pass http://www_pools;
include proxy_params;
}
}
[root@lb01 ~]# curl www.test.com
web02 www
[root@lb01 ~]# curl www.test.com
web02 www
[root@lb01 ~]# curl www.test.com
web01 www
- ip_hash(通过访问者的IP进行算法分配,IP地址不变请求的服务器不变)
upstream www_pools {
ip_hash;
server 172.16.1.7 ;
server 172.16.1.8 ;
}
- url_hash(和ip_hash差不多,不过是请求内容来进行计算,内容不变,请求服务器不变,可能造成负载不均衡的现象)
upstream www_pools {
hash $request_uri;
server 172.16.1.7 ;
server 172.16.1.8 ;
}
- 用于测试访问命令
for i in {1..100};do curl -s -H "host:www.mysun.com" 127.0.0.1;done |grep web02|wc -l
- down参数
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf
upstream www_pools{
server 172.16.1.51:8080 down; #down表示可以在不关闭后端WEB服务器服务去情况下,在负载停止填写了down服务器的转发。
server 172.16.1.52:8080;
}
server {
listen 80;
server_name www.test.com bbs.test.com;
location / {
proxy_pass http://www_pools;
include proxy_params;
}
}
- backup参数
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy.conf
upstream www_pools{
server 172.16.1.51:8080 backup; #backup至此台IP服务器是备选的,当地址池中172.16.1.52Nginx服务挂了,才启动备选服务器。当172.16.1.52服务恢复51又将成为备选服务器
server 172.16.1.52:8080;
}
server {
listen 80;
server_name www.test.com bbs.test.com;
location / {
proxy_pass http://www_pools;
include proxy_params;
}
}
8.根据条件转发实战
根据客户端类型转发
需求:
如果用户是iphone就跳转到iphone页面
如果用户是安卓就跳转到安卓页面
如果用户是pc就跳转到pc页面
如果用户是IE就返回403
- web服务器nginx配置
[root@web02 /etc/nginx/conf.d]# cat test.conf
server {
listen 8080;
server_name www.sj.com;
location / {
root /code/android;
index index.html;
}
}
server {
listen 8081;
server_name www.sj.com;
location / {
root /code/iphone;
index index.html;
}
}
server {
listen 8082;
server_name www.sj.com;
location / {
root /code/pc;
index index.html;
}
}
- 生成测试页面(两台机都执行)
mkdir -p /code/{android,iphone,pc}
echo "$(hostname) PC" > /code/pc/index.html
echo "$(hostname) Iphone" > /code/iphone/index.html
echo "$(hostname) Android" > /code/android/index.html
nginx -t
systemctl restart nginx
- lb 服务器配置
[root@lb01 /etc/nginx/conf.d]# cat js.conf
upstream android {
server 172.16.1.51:8080;
}
upstream iphone {
server 172.16.1.52:8081;
}
upstream pc {
server 172.16.1.51:8082;
server 172.16.1.52:8082;
}
server {
listen 80;
server_name www.js.com;
location / {
#默认跳转至 pc 站点
proxy_pass http://pc;
include proxy_params;
#如果客户端是 Iphone 则跳转到 iphone 的资源池,~*不区分大小写的正则匹配
if ($http_user_agent ~* "Iphone") {
proxy_pass http://iphone;
}
#如果客户端是 Android 则跳转到 android 的资源池
if ($http_user_agent ~* "Android"){
proxy_pass http://android;
}
#如果客户端是 IE 浏览器,则返回 403 错误。
if ($http_user_agent ~* "msie"){
return 403;
}
}
}
nginx -t
systemctl restart nginx
- 访问测试(curl命令会显示使用的浏览器,也可以模拟自定义)
echo "10.0.0.5 www.js.com" >> /etc/hosts
curl -A "iphone" www.js.com
curl -A "android" www.js.com
curl -A "msie" www.js.com
实战2
需求:
访问图片格式就跳转到web01
访问其他地址就跳转到web02
- web服务器配置(两太一样)
[root@web01 /code]# cat /etc/nginx/conf.d/tp.conf
server {
listen 80;
server_name tp.test.com;
location / {
root /code;
index index.html;
}
}
nginx -t
systemctl restart nginx
- 生成测试页面(图片只需web01下载即可)
echo "$(hostname) www" > /code/index.html
cd /code/ && wget -O sun.jpg http://pic.51yuansu.com/pic3/cover/02/27/64/59c008e1c7954_610.jpg
-lb服务器配置
[root@lb01 /etc/nginx/conf.d]# cat jpg.conf
upstream static {
server 172.16.1.51; #有图片的服务器
}
upstream default {
server 172.16.1.52;
}
server {
listen 80;
server_name tp.test.com; #一个server可以包含多个location
location / {
proxy_pass http://default;
include proxy_params;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { #匹配包含这些字符的调整到static地址池
proxy_pass http://static;
include proxy_params;
}
}
nginx -t
systemctl restart nginx
-
访问测试
image.png
image.png