文章目录
- 前言
- 一、反向代理具体是什么
- 反向代理的配置参数proxy_pass
- 二、负载均衡
- 轮询(round robin)(默认调度算法)
- 加权轮询(weight round robin)
- ip_hash
- 最少连接数 (least_conn)
- fair(需编译安装第三方模块 ngx_http_upstream_fair_module)
- url_hash(需编译安装第三方模块 ngx_http_upstream_hash_module)
- 三、实际操作实现
前言
nginx的基本认识,以及conf配置文件的认识,请参考上一篇文章Nginx反向代理与系统参数配置conf 本文主要是回顾今日所学,简单实现nginx反向代理和负载均衡
一、反向代理具体是什么
反向代理:看下面原理图,就一目了然。客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP地址。
反向代理的配置参数proxy_pass
proxy_pass是location里的一个配置项 ,意思是将当前命中的 Nginx 接口(例如:http://localhost/api )代理到其他 server 的接口,如下例子就是将 http://localhost/api 代理到 https://baidu.com/api
location /api {
proxy_pass https://baidu.com;
}
需要注意的是,在写 proxy_pass 不能随便在目标地址后加 /,如果你在地址末尾加了 / ,则最终代理是这样的:
location /api {
proxy_pass https://baidu.com/; # 将会被代理到 https://baidu.com/,后面没有 /api
}
不加 /,则最终代理是这样的,访问 Nginx 命中的 /api ,Nginx 也会自动帮你拼接上去:
location /api {
proxy_pass https://baidu.com; # 将会代理到 https://baidu.com/api
}
二、负载均衡
负载均衡其意思就是分摊到多个操作单元上进行执行。
在访问量较多的时候,可以通过负载均衡,将多个请求分摊到多台服务器上,相当于把一台服务器需要承担的负载量交给多台服务器处理,进而提高系统的吞吐率;另外如果其中某一台服务器挂掉,其他服务器还可以正常提供服务,以此来提高系统的可伸缩性与可靠性。
nginx本身支持4种:轮询(round robin), 加权轮询(weight round robin), ip_hash, 最小连接(least_conn)
第三方模块,支持2种:最快响应时间(fair), url_hash
轮询(round robin)(默认调度算法)
特点:每个请求按时间顺序逐一分配到不同的后端服务器处理。
适用业务场景:后端服务器硬件性能配置完全一致,业务无特殊要求时使用。
upstream bertram.com {
server 192.168.0.14:8080 max_fails=2 fail_timeout=10s;
server 192.168.0.15:8080 max_fails=2 fail_timeout=10s;
}
加权轮询(weight round robin)
特点:指定轮询几率,weight值(权重)和访问比例成正比,用户请求按权重比例分配。
适用业务场景:用于后端服务器硬件性处理能力不平均的情形。
upstream bertram.com {
server 192.168.0.14:8080 weight=5 max_fails=2 fail_timeout=10s;
server 192.168.0.15:8080 weight=10 max_fails=2 fail_timeout=10s;
}
ip_hash
特点:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session会话保持问题。
适用业务场景:适用于需要账号登录的系统,会话连接保持的业务。
upstream bertram.com {
ip_hash;
server 192.168.0.14:8080 max_fails=2 fail_timeout=10s;
server 192.168.0.15:8080 max_fails=2 fail_timeout=10s;
}
最少连接数 (least_conn)
特点:按nginx反向代理与后端服务器之间的连接数,连接数最少的优先分配。
适用业务场景:适用于客户端与后端服务器需要保持长连接的业务。
upstream bertram.com {
least_conn;
server 192.168.0.14:8080 max_fails=2 fail_timeout=10s;
server 192.168.0.15:8080 max_fails=2 fail_timeout=10s;
}
fair(需编译安装第三方模块 ngx_http_upstream_fair_module)
特点:按后端服务器的响应时间来分配请求,响应时间短的优先分配。
适用业务场景:对访问响应速度有一定要求的业务。
upstream bertram.com {
fair;
server 192.168.0.14:8080 max_fails=2 fail_timeout=10s;
server 192.168.0.15:8080 max_fails=2 fail_timeout=10s;
}
url_hash(需编译安装第三方模块 ngx_http_upstream_hash_module)
特点:按访问url的hash结果来分配请求,使同一个url访问到同一个后端服务器。
适用业务场景:适用于后端服务器为缓存服务器时比较有效。
upstream bertram.com {
server 192.168.0.14:8080 max_fails=2 fail_timeout=10s;
server 192.168.0.15:8080 max_fails=2 fail_timeout=10s;
hash $request_uri;
}
三、实际操作实现
练习负载均衡、反向代理实现,需要多台虚拟机来实现。
因自己电脑性能太拉胯,只能通过固定ip+变换端口来进行模拟
conf配置文件的配置如下:
upstream httpd{
server 192.168.112.129:81;
server 192.168.112.129:82;
server 192.168.112.129:83;
server 192.168.112.129:84;
}
server {
listen 80;
server_name www.liuheming.com;
location / {
proxy_pass http://httpd;
# root /www/www;
# index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server{
listen 84;
server_name mmm.liuheming.com;
location /{
root /www/vod;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location =/50x.html{
root html;
}
}
server{
listen 81;
server_name localhost;
location /{
root /www/vid;
index index.html index.htm;
}
}
server{
listen 82;
server_name localhost;
location /{
root /www/ccc;
index index.html index.htm;
}
}
server{
listen 83;
server_name localhost;
location /{
root /www/eee;
index index.html index.htm;
}
}
上面中的,www.liuheming.com为在阿里买的liuheming.com的域名,绑定的主机ip的域名
运行结果如下: