反向代理负载均衡 第1章 反向代理 Nginx代理服务基本概述 说到代理,首先我们要明确一个概念,所谓代理就是一个代表、一个渠道;
此时就设计到两个角色,一个是被代理角色,一个是目标角色,被代理角色通过这个代理访问目标角色完成一些任务的过程称为代理操作过程;如同生活中的专卖店~客人到adidas专卖店买了一双鞋,这个专卖店就是代理,被代理角色就是adidas厂家,目标角色就是用户
1.代理一词往往并不陌生, 该服务我们常常用到如(代理理财、代理租房、代理收货等等),如下图所示
2.在没有代理模式的情况下,客户端和 Nginx 服务端,都是客户端直接请求服务端,服务端直接响应客户端。
3.那么在互联网请求里面, 客户端往往无法直接向服务端发起请求, 那么就需要用到代理服务, 来实现客户端和服务通信,如下图所示
正向代理和反向代理 以访问Goo为例,客户端连接到VPN相当于正向代理 VPN代理请求访问后端服务器并返回属于反向代理
Nginx代理服务支持的协议 1.Nginx 作为代理服务,可支持的代理协议非常的多,具体如下图
2.如果将 Nginx 作为反向代理服务,常常会用到如下几种代理协议,如下图所示
3.反向代理模式与 Nginx 代理模块总结如表格所示
反向代理模式
nginx配置模块
http、websocket、https
ngx_http_proxy_module
fastcgi
ngx_http_fastcgi_module
uwsgi
ngx_http_uwsgi_module
grpc
ngx_http_v2_module
Nginx反向代理配置语法 1.Nginx代理配置语法
1 2 3 4 5 6 Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except http://localhost:8000/uri/ http://192.168.56.11:8000/uri/ http://unix:/tmp/backend.socket:/uri/
2.添加传递给后端服务器的请求头信息
1 2 3 4 5 6 7 8 9 10 Syntax: proxy_set_header field value; Default: proxy_set_header Host $proxy_host ; proxy_set_header Connection close; Context: http, server, location proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
3.代理到后端的TCP连接数,响应,返回等超时时间
1 2 3 4 5 6 7 8 9 10 11 12 //nginx 代理与后端服务器连接超时时间(代理连接超时) Syntax: proxy_connect_timeout time; Default: proxy_connect_timeout 60s; Context: http, server, location //nginx 代理等待后端服务器的响应时间 Syntax: proxy_read_timeout time; Default: proxy_read_timeout 60s; Context: http, server, location //后端服务器数据回传给 nginx 代理超时时间 Syntax: proxy_send_timeout time; Default: proxy_send_timeout 60s; Context: http, server, location
4.代理缓冲区
1 2 3 4 5 6 7 8 9 10 11 12 //nignx 会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端 Syntax: proxy_buffering on | off; Default: proxy_buffering on; Context: http, server, location //设置 nginx 代理保存用户头信息的缓冲区大小 Syntax: proxy_buffer_size size; Default: proxy_buffer_size 4k|8k; Context: http, server, location //proxy_buffers 缓冲区 Syntax: proxy_buffers number size; Default: proxy_buffers 8 4k|8k; Context: http, server, location
5.proxy代理网站常用配置 将配置写入新文件,调用时使用include引用即可
1 2 3 4 5 6 7 8 9 10 [root@lb01 ~] proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k;
6.代理配置location时调用,方便后面多个location重复使用
1 2 3 4 location / { proxy_pass http://127.0.0.1:8080; include proxy_params; }
Nginx反向代理实战 配置关系图:
1.环境准备
主机名
IP
服务
web01
172.16.1.7
web服务器
lb01
10.0.1.5
代理服务器
2.web01服务器配置 配置一个网站,监听在 8080,此时网站仅 172 网段的用户能访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@web01 ~] [root@web01 /etc/nginx/conf.d] server { listen 8080; server_name www.oldboy.com; access_log /var/log /nginx/www.access.log main; location / { root /code/www; index index.html index.htm; charset utf-8,gbk; deny 10.0.1.0/24; allow all; } } [root@web01 conf.d] [root@web01 conf.d] [root@lb01-5 /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb01-5 /etc/nginx/conf.d]
3.配置lb01代理服务器 配置监听 eth0 的 80 端口,使 10.0.1.0 网段的用户,能够通过代理服务器访问到后端的172.16.1.7 的 8080 端口站点内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@lb01-5 /etc/nginx] proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k; [root@lb01-5 /etc/nginx/conf.d] server { listen 80; server_name www.oldboy.com; location / { proxy_pass http://172.16.1.7:8080; include proxy_params; } } [root@lb01-5 /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb01-5 /etc/nginx/conf.d]
第2章 负载均衡 Nginx负载均衡概述 2.1 为什么需要负载均衡 我们的 Web 服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台 WEB 服务器组成集群,前端使用 Nginx 负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾
往往我们接触的最多的是 SLB(Server Load Balance)负载均衡,实现最多的也是 SLB、那么 SLB 它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。 所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx 则是一个典型的 SLB
2.2 Nginx负载均衡配置场景 2.2.1 四层负载均衡 所谓四层负载均衡指的是 OSI 七层模型中的传输层,那么传输层 Nginx 已经能支持 TCP/IP 的控制,所以只需要对客户端的请求进行 TCP/IP 协议的包转发就可以实现负载均衡,那么它的好处是性能非常快、只需要底层进行应用处理,而不需要进行一些复杂的逻辑
2.2.2 七层负载均衡 七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,比如我们说的 http 应用的负载均衡,它可以实现 http 信息的改写、头信息的改写、安全应用规则控制、 URL 匹配规则控制、以及转发、 rewrite 等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么 Nginx 则是一个典型的七层负载均衡 SLB
2.2.3 四层负载均衡与七层负载均衡区别 四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发、由此可以看出,七层负载均衡效率没有四负载均衡高。 但七层负载均衡更贴近于服务,如:http 协议就是七层协议,我们可以用 Nginx 可以作会话保持, URL 路径规则匹配、 head 头改写等等,这些是四层负载均衡无法实现的
Nginx负载均衡调度算法
调度算法
概述
轮询
按时间顺序逐一分配到不同的后端服务器(默认)
weight
加权轮询,weight值越大,分配到的访问几率越高
ip_hash
每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash
按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn
z最少链接数,那个机器链接数少就分发
Nginx负载均衡配置参数
状态
概述
down
当前的server暂时不参与负载均衡
backup
预留的备份服务器
max_fails
允许请求失败的次数
fail_timeout
经过max_fails失败后,服务暂停时间
max_conns
限制最大的接受链接数
2.3 Nginx负载均衡实战 1.部署准备
/static
10.0.1.7:8080
static静态服务器
/upload
10.0.1.8:8080
upload服务器
/
10.0.1.9:8080
默认服务器
2.创建地址池
1 2 3 4 5 6 7 8 9 10 11 upstream upload_pools{ server 10.0.0.8:8080; } upstream static_pools{ server 10.0.0.7:8080; } upstream defaule_pools{ server 10.0.0.9:8080; }
3.匹配条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 location /static/ { proxy_pass http://static_pools; include proxy_params; } location /upload/ { proxy_pass http://upload_pools; include proxy_params; } location / { proxy_pass http://default_pools; include proxy_params; }
4.组合在一起
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 [root@lb01-5 /etc/nginx/conf.d] upstream static_pools { server 172.16.1.7:8080; } upstream upload_pools { server 172.16.1.8:8080; } upstream default_pools { server 172.16.1.9:8080; } server { listen 80; server_name www.oldboy.com; location /static/ { proxy_pass http://static_pools; include proxy_params; } location /upload/ { proxy_pass http://upload_pools; include proxy_params; } location / { proxy_pass http://default_pools; include proxy_params; } } [root@lb01-5 /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb01-5 /etc/nginx/conf.d]
5.创建代码环境
每个虚拟机存放的网页路径
1 2 3 www.oldboy.com/index.html www.oldboy.com/upload/index.html www.oldboy.com/static/index.html
创建目录及测试页面命令
1 2 3 4 5 mkdir -p /code/www/{upload,static} echo "$(hostname) default" >/code/www//index.html echo "$(hostname) upload" >/code/www/upload/index.html echo "$(hostname) static" >/code/www/static/index.html chown -R www.www /code
6.进行测试
1 2 3 4 5 6 [root@lb01-5 /etc/nginx/conf.d] web03 default [root@lb01-5 /etc/nginx/conf.d] web01 static [root@lb01-5 /etc/nginx/conf.d] web02-8 upload
第3章 根据条件转发实战 3.1 根据文件类型转发 转发需求
只需修改nginx.conf的配置文件中的loaction区块代码即可
lb01配置文件
1 2 3 4 5 6 7 8 9 10 11 location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { proxy_pass http://static_pools; proxy_set_header Host $host ; proxy_set_header X-Forwarded-For $remote_addr ; } upstream server_pools { server 10.0.0.7 weight=1 max_fails=3 fail_timeout=10s; server 10.0.0.8 weight=1 max_fails=3 fail_timeout=10s; server 10.0.0.9 weight=1 max_fails=3 fail_timeout=10s; }
3.2 动静分离 转发需求
动态资源转发到php服务器 静态资源转发到静态服务器
lb01配置文件
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 [root@lb01-5 /etc/nginx/conf.d] upstream static_pools { server 172.16.1.7:8080; } upstream upload_pools { server 172.16.1.8:8080; } upstream default_pools { server 172.16.1.9:8080; } server { listen 80; server_name www.oldboy.com; location /static/ { proxy_pass http://static_pools; include proxy_params; } location /upload/ { proxy_pass http://upload_pools; include proxy_params; } location / { proxy_pass http://default_pools; include proxy_params; } location ~ .*\.(png|jpg|gif)$ { proxy_pass http://static_pools; include proxy_params; } } [root@lb01-5 /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb01-5 /etc/nginx/conf.d]
3.3 根据客户端转发 转发需求
手机和电脑 访问相同的网站—-结果不同
lb01代理服务器配置文件
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 [root@lb01-5 /etc/nginx/conf.d] upstream static_pools { server 172.16.1.7:8080; } upstream upload_pools { server 172.16.1.8:8080; } upstream default_pools { server 172.16.1.9:8080; } upstream iphone_pools { server 172.16.1.7:8080; } upstream android_pools { server 172.16.1.8:8080; } server { listen 80; server_name www.oldboy.com; location /static/ { proxy_pass http://static_pools; include proxy_params; } location /upload/ { proxy_pass http://upload_pools; include proxy_params; } location / { proxy_pass http://default_pools; include proxy_params; if ($http_user_agent ~* "Iphone" ) { proxy_pass http://iphone_pools; } if ($http_user_agent ~* "Android" ){ proxy_pass http://android_pools; } if ($http_user_agent ~* "msie" ){ return 403; } } location ~ .*\.(png|jpg|gif)$ { proxy_pass http://static_pools; include proxy_params; } } [root@lb01-5 /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb01-5 /etc/nginx/conf.d]
访问测试
1 2 3 4 5 6 7 8 9 [root@web01 /code/www] [root@web02-8 /code/www] [root@lb01-5 /etc/nginx/conf.d] web03 default [root@lb01-5 /etc/nginx/conf.d] web01 iphone [root@lb01-5 /etc/nginx/conf.d] web02 android