nginx超详细讲解之location,rewrite,反向代理及负载均衡

9 篇文章 0 订阅

原文转载:https://blog.csdn.net/u014459326/article/details/53367333

一、location 的语法

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

1.1location分类及用法

location大致分为三类:

location = patt {} [精准匹配]

location patt{} [一般匹配]

location ~ patt{} [正则匹配]

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

1.2location执行流程

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

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

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

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

二、rewrite重写

2.1重写中用到的指令

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

set #设置变量

return #返回状态码

break #跳出rewrite

rewrite #重写

2.2语法格式

If 语法格式

If 空格 (条件) {

重写模式

}


条件又怎么写?

答:3种写法

1: “=”来判断相等, 用于字符串比较

2: “~” 用正则来匹配(此处的正则区分大小写)

~* 不区分大小写的正则

3: -f -d -e来判断是否为文件,为目录,是否存在.

例子:

  1. if ($remote_addr = 192.168.1.100) {
  2. return 403;
  3. }
  4. if ($http_user_agent ~ MSIE) {
  5. rewrite ^.*$ /ie.htm;
  6. break; #(不 break会循环重定向)
  7. }
  8. if (!-e $document_root$fastcgi_script_name) {
  9. rewrite ^.*$ / 404.html break;
  10. }
  11. if ($http_user_agent ~* msie) {
  12. set $isie 1;
  13. }
  14. if ($fastcgi_script_name = ie.html) {
  15. set $isie 0;
  16. }
  17. if ($isie 1) {
  18. rewrite ^.*$ ie.html;
  19. }

三、反向代理及负载均衡

3.1反向代理

1.主要使用proxy_pass方法

例如:

  1. location ~ .*\.(jpg|png|gif|bmp)$ {
  2. proxy_pass http: //imagesever;
  3. }
  4. upstream imagesever {
  5. server 127.0 .0 .1: 8080 weight= 1 max_fails= 2 fail_timeout= 30s;
  6. server 127.0 .0 .1: 8081 weight= 1 max_fails= 2 fail_timeout= 30s;
  7. }

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和访问比率成正比,用于后端服务器性能不均的情况。

例如:

  1. upstream bakend {
  2. server 192.168 .0 .14 weight= 10;
  3. server 192.168 .0 .15 weight= 10;
  4. }
3、ip_hash

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

例如:

  1. upstream bakend {
  2. ip_hash;
  3. server 192.168 .0 .14: 88;
  4. server 192.168 .0 .15: 80;
  5. }
4、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

  1. upstream backend {
  2. server server1;
  3. server server2;
  4. fair;
  5. }
5、url_hash(第三方)

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

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

  1. upstream backend {
  2. server squid1: 3128;
  3. server squid2: 3128;
  4. hash $request_uri;
  5. hash_method crc32;
  6. }
tips:

  1. upstream bakend{#定义负载均衡设备的Ip及设备状态
  2. ip_hash;
  3. server 127.0 .0 .1: 9090 down;
  4. server 127.0 .0 .1: 8080 weight= 2;
  5. server 127.0 .0 .1: 6060;
  6. server 127.0 .0 .1: 7070 backup;
  7. }

在需要使用负载均衡的server中增加

proxy_pass http://bakend


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值