nginx 负载均衡亲测

一、准备阶段,现在准备四台服务器

1、192.168.1.2   tomcat服务器1;

2、192.168.1.3  tomcat 服务器2:

3、192.168.1.200 nginx服务器;


二、nginx配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   upstream tomcat{
       server 192.168.1.2:8080;  //进行负载的2台主机
       server 192.168.1.3:8080;
   }
    server{
      listen 80;
      server_name hello.com;  //访问域名
      location /{
            proxy_pass http://tomcat;  //加入upstream配置的名字,这样从负载均衡就生效了
      }

    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
 location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}

三、配置hello.com域名

 配置host文件 ,路径是:C:\Windows\System32\drivers\etc,配置文件:192.168.1.200   hello.com


四、负载均衡访问:

打开域名:hello.com ,就可以访问配置的TOMCAT2个服务器。


五、nginx 的upstream目前支持4种方式的分配

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

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

 upstream bakend {        
   server 192.168.0.2 weight=10;      
   server 192.168.0.3 weight=3;    
}

3ip_hash 
  
每个请求按访问iphash结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。这个必须配置,否则则取不到session,例如:

   upstream bakend {
         ip_hash;
         server192.168.0.14:88;
         server192.168.0.15:80;
    }
 

4fair(第三方)  按后端服务器的响应时间来分配请求,响应时间短的优先分配。

  upstream backend {
         server server1;
         server server2;
         fair;
  }
5、Nginx 调度策略 - 小技巧

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/ ;

小技巧参数说明为:
1.down 
表示当前server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大

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

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

5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡.


六、proxy_pass后的url加不加/的区别

下面四种情况分别用http://hello.com/css/test.html 进行访问。
第一种:
location  /css/ {
          proxy_pass http://tomcat/;
}
会被代理到http://192.168.134.142:8080/test.html 这个url 


第二种(相对于第一种,最后少一个 /)
location  /css/ {
          proxy_pass http://tomcat;
}
会被代理到http://192.168.134.142:8080/css/test.html 这个url 


第三种:
location  /css/ {
          proxy_pass http://tomcat/js/;
}
会被代理到http://192.168.134.142:8080/js/test.html 这个url。


第四种情况(相对于第三种,最后少一个 /)

location  /css/ {

          proxy_passhttp://tomcat/js;

}

会被代理到http://192.168.134.142:8080/jstest.html这个url

 

带“/”就会把 http://servername:port/path/替换掉


七、NginxURL进行匹配

语法规则: location [=|~|~*|^~]/uri/ { … }
= 开头表示精确匹配 
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。 
~ 开头表示区分大小写的正则匹配 
~*  开头表示不区分大小写的正则匹配 
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则 
/ 通用匹配,任何请求都会匹配到。 
多个location配置的情况下匹配顺序为: 
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
 “$”符号表示URL结尾.

例子,有如下匹配规则例子:
location = / {
#规则A
        proxy_pass http://tomcat/index;
}
location = /login {
#规则B
        proxy_pass http://tomcat/login.jsp;
}
location ^~ /static/ {
#规则C
        root   /usr/share/nginx/html;
        index  index.html index.htm;
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D
        root   /usr/share/nginx/html;
}

location ~* \.png$ {

#规则E

       proxy_passhttp://tomcat;

}

location ~\.shtml$ {

#规则F

       root   /usr/share/nginx/html;

      if (!-e $request_filename){

        proxy_passhttp://tomcat;

      }

}

location ~*\.shtml$ {

#规则G

        root   /usr/share/nginx/html;

      if (!-e $request_filename){

        proxy_passhttp://tomcat;

        }

}

location / {

#规则H

     proxy_passhttp://tomcat;

}

访问根目录/, 比如http://hello.com/ 将匹配规则A

访问http://hello.com/login将匹配规则Bhttp://hello.com/register则匹配规则H

访问http://hello.com/static/a.html将匹配规则C

访问http://hello.com/a.gif,http://hello.com/b.jpg将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而http://hello.com/static/c.png则优先匹配到 规则C

访问http://hello.com/a.PNG则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。

访问http://hello.com/a.shtml不会匹配规则F和规则Ghttp://hello.com/a.SHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。

访问http://hello.com/category/id/1111则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGIphp),tomcatjsp),nginx作为方向代理服务器存在。


NginxURL进行匹配-常用


 所以实际使用中,至少有三个匹配规则定义常用,如下: 
直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat/index

# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat/
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值