天天看点

nginx 之 location,rewrite,反向代理及负载均衡nginx 之 location,rewrite,反向代理及负载均衡

nginx 之 location,rewrite,反向代理及负载均衡

一、location 的语法

locltion可以把不同方式的请求,定位到不同的处理方式上(个人感觉有点像java中的filter)

1.1 location分类及用法

location大致分为三类:

location = patt {} [精准匹配]

location patt{} [一般匹配]

location ~ patt{} [正则匹配]
           
location / {//定位,个人理解就是java中的filter。
            root   html;   //符合条件请求转发路径
            index  index.html index.htm;      //索引
        }
           
如果访问的页面为xxx.com
首先因为访问的是/会进入该location然后给出的索引为index.html
接下来回去访问/index.html继续命中该location
然后到html路径下去寻找index.html
root为资源存放的目录,该例子是放在html(nginx安装根目录下的html文件夹的相对路径),可以使用/开头表示绝对路径
index为索引
           

1.2 location执行流程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xo9KoPxn-1578729094199)(https://github.com/bigdatajava/blogspot/raw/master/img/tuchuang/nginx.jpg)]

1、首先查看精准匹配是否匹配成功,如果成功则返回结果并停止解析过程

2、查看普通匹配是否匹配成功,如果匹配成功,把匹配成功最长的记录下来

3、依次寻找正则匹配,如果有一个匹配,立马返回结果并停止解析过程

4、如果正则都不匹配则返回普通匹配中记录的最长匹配
           

二、rewrite重写

2.1 重写中用到的指令

if (条件) {} 设定条件,再进行重写

set #设置变量

return #返回状态码

break #跳出rewrite

rewrite #重写
           

2.2 语法格式

If 语法格式:
If 空格 (条件) {
	重写模式
}

条件又怎么写?
答:3种写法

1: “=”来判断相等, 用于字符串比较
2: “~” 用正则来匹配(此处的正则区分大小写) 
	~* 不区分大小写的正则
3: -f -d -e来判断是否为文件,为目录,是否存在.
           

例子:

if ($remote_addr = 192.168.1.100) {
return 403;
}
if ($http_user_agent ~ MSIE) {
rewrite ^.*$ /ie.htm;
break; #(不break会循环重定向)
}
if (!-e $document_root$fastcgi_script_name) {
rewrite ^.*$ /404.html break;
} 
if ($http_user_agent ~* msie) {
set $isie 1;
}
if ($fastcgi_script_name = ie.html) {
set $isie 0;
}
if ($isie 1) {
rewrite ^.*$ ie.html;
}
           

三、反向代理及负载均衡

3.1 反向代理

1) 主要使用proxy_pass方法

例如:

location ~ .*\.(jpg|png|gif|bmp)$ {
	proxy_pass http://imagesever;
}

upstream imagesever {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
           

2) upstream每个设备的状态说明

1.down 表示单前的server暂时不参与负载

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

3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误

4.fail_timeout:max_fails次失败后,暂停的时间。

5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
           

3) upstream负载均衡方式

nginx的upstream目前支持5种方式的分配

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
           

2、weight

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

例如:

upstream bakend {
    server 192.168.0.14 weight=10;
    server 192.168.0.15 weight=10;
}
           

3、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
           

例如:

upstream bakend {
	ip_hash;
    server 192.168.0.14:88;
    server 192.168.0.15:80;
}
           

4、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。
           
upstream backend {
    server server1;
    server server2;
    fair;
}
           

5、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
           

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
}
tips:
upstream bakend{	#定义负载均衡设备的Ip及设备状态
    ip_hash;
    server 127.0.0.1:9090 down;
    server 127.0.0.1:8080 weight=2;
    server 127.0.0.1:6060;
    server 127.0.0.1:7070 backup;
}

在需要使用负载均衡的server中增加
proxy_pass http://bakend/;
           

继续阅读