三十九、负载均衡及扫尾技术知识

一.nginx实现负载均衡或者代理

        1.  在前面我们已经说过,安装nginx许多核心模块已经安装,nginx的配置文件是以模块的形式组织的,不同的模块实现不同的功能,其中代理或者说负载是upstream_module模块实现的,所以我们首先要安装nginx,通过./configure文件时,我们可以看到在生成makefile的时候,默认是安装了upstream_modul。

    [root@proxy nginx-1.6.2]# ./configure --help |grep upstream
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module

       2.在nginx配置文件中进行配置:/application/nginx/conf的nginx.conf文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {
     server 192.168.0.101:80  max_fails=3 fail_timeout=30s;
     server 192.168.0.103:80  max_fails=3 fail_timeout=30s;
     server 192.168.0.104:80  max_fails=3 fail_timeout=30s;
}  #------表示需要进行负载均衡转发的机器,upstream运用自己的机制,进行随机转发
    server {
        listen       80;
        server_name  www.etiantian.org;
        index  index.html index.htm;
        location / {
         proxy_pass http://backend;
    }  #------location / 表示如果没有其它url,默认情况下执行它,那么通过proxy_pass模块确定是负载 ,调用函数backend,即找到upstream负载运行机制,随机调用函数
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
   3.测试负载均衡成功:链接192.168.0.104会随机转发到192.168.0.103或者192.168.0.105上面
   

猜你喜欢

转载自www.cnblogs.com/dangjingwei/p/11324484.html