Skip to content

六、基于反向代理的负载均衡

nginx
# upstream 和 server标签同级
upstream httpd {
    server 192.168.44.102:80;
    server 192.168.43.103:80;
}

location / {
    proxy_pass http://httpd;
}

1、负载均衡策略

1、轮询

默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。

2、weight(权重)

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

nginx
upstream httpd { 
	server 127.0.0.1:8050 weight=10 down; 
	server 127.0.0.1:8060 weight=1; 
	server 127.0.0.1:8060 weight=1 backup; 
}
  • down:表示当前的server暂时不参与负载

  • weight:默认为1.weight越大,负载的权重就越大。

  • backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。

3、ip_hash

根据客户端的ip地址转发同一台服务器,可以保持回话。

4、least_conn

最少连接访问

5、url_hash

根据用户访问的url定向转发请求

6、fair

根据后端服务器响应时间转发请求

2、动静分离

1、配置反向代理

nginx
 location / {
    proxy_pass http://127.0.0.1:8080;
    root   html;
    index  index.html index.htm;
}

2、增加每一个location

nginx
  location /css {
      root   /usr/local/nginx/static;
      index  index.html index.htm;
  }
	location /images {
      root   /usr/local/nginx/static;
      index  index.html index.htm;
  }
	location /js {
      root   /usr/local/nginx/static;
      index  index.html index.htm;
  }

3、使用一个location

使用正则

location 前缀

前缀描述
=开头表示精确匹配
^~开头表示 uri 以某个常规字符串开头,理解为匹配 url 路径即可。nginx 不对 url 做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
~正则匹配,区分大小写
~*正则匹配,不区分大小写
/通用匹配,任何请求都会匹配到

location匹配顺序

  • 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配

  • 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)

  • 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配

  • 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)

nginx
location ~*/(css|img|js) {
     root   /usr/local/nginx/static;
     index  index.html index.htm;
 }

alias与root

nginx
location /css {
    alias   /usr/local/nginx/static/css;
    index  index.html index.htm;
}

root用来设置根目录,而alias在接受请求的时候在路径上不会加上location。

1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;

2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;

3)使用alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上"/"符号!!

4)alias虚拟目录配置中,location匹配的path目录如果后面不带"/",那么访问的url地址中这个path目录后面加不加"/"不影响访问,访问时它会自动加上"/"; 但是如果location匹配的path目录后面加上"/",那么访问的url地址中这个path目录必须要加上"/",访问时它不会自动加上"/"。如果不加上"/",访问就会失败!

5)root目录配置中,location匹配的path目录后面带不带"/",都不会影响访问。

3、UrlRewrite

1、rewrite语法格式及参数语法

sh
rewrite是实现URL重写的关键指令,根据regex (正则表达式)部分内容, 
重定向到replacement,结尾是flag标记。

rewrite   <regex>   <replacement>   [flag]; 
关键字       正则       替代内容      flag标记

关键字:其中关键字error_log不能改变
正则:perl兼容正则表达式语句进行规则匹配 
替代内容:将正则匹配的内容替换成replacement 
flag标记:rewrite支持的flag标记
rewrite参数的标签段位置: 
server,location,if

flag标记说明:
last #本条规则匹配完成后,继续向下匹配新的location URI规则 
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址 
permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

实例

sh
rewrite   ^/([0-9]+).html$     /index.jsp?pageNum=$1    break;

2、同时使用负载均衡

应用服务器防火墙配置

开启防火墙

sh
systemctl start firewalld

重启防火墙

sh
systemctl restart firewalld

重载规则

sh
firewall-cmd --reload

查看已配置规则

sh
firewall-cmd --list-all

指定端口和ip访问

sh
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.44.101"
port protocol="tcp" port="8080" accept"

移除规则

sh
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source
address="192.168.44.101" port port="8080" protocol="tcp" accept"

网关配置

nginx
upstream httpds {
server 192.168.44.102 weight=8 down;
server 192.168.44.103:8080 weight=2;
server 192.168.44.104:8080 weight=1 backup;
}
		location / {
     	rewrite ^/([0-9]+).html$             /index.jsp?pageNum=$1      redirect;
     	proxy_pass http://httpds ;
     }

3、防盗链配置

sh
valid_referers none | blocked | server_names | strings ....;
  • none, 检测 Referer 头域不存在的情况。

  • blocked,检测 Referer 头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以“http://” 或 “https://” 开头。

  • server_names ,设置一个或多个 URL ,检测 Referer 头域的值是否是这些 URL 中的某一个。

在需要防盗链的location中配置

nginx
valid_referers 192.168.44.101; 
	if ($invalid_referer) { 
	return 403; 
}


valid_referers none 192.168.44.101; 
	if ($invalid_referer) { 
	return 403; 
}


# 自定义错误页面
valid_referers none 192.168.44.101; 
	if ($invalid_referer) { 
	return 401; 
}

error_page 401 /401.html;
location = /401.html{
  root html
}

# 整合rewrite自定义返回报错图片
valid_referers none 192.168.44.101; 
	if ($invalid_referer) { 
		rewrite ^/ /img/xxx.png break;
}

使用curl测试

sh
# -I 返回头信息
curl -I http://192.168.44.101/img/logo.png

带引用

sh
curl -e "http://baidu.com" -I http://192.168.44.101/img/logo.png