Centos7 nginx的负载均衡概念与配置

.负载均衡概念

负载均衡(Server Load Balancer)是将访问流量根据转发策略分发到后端多台 ECS 的流量分发控制服务。负载均衡可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。

负载均衡主要有如下几个功能点:

(1)负载均衡服务通过设置虚拟服务地址(IP),将位于同一地域(Region)的多台云服务器(Elastic Compute Service,简称ECS)资源虚拟成一个高性能、高可用的应用服务池;根据应用指定的方式,将来自客户端的网络请求分发到云服务器池中。

(2)负载均衡服务会检查云服务器池中ECS的健康状态,自动隔离异常状态的ECS,从而解决了单台ECS的单点问题,同时提高了应用的整体服务能力。在标准的负载均衡功能之外,负载均衡服务还具备TCPHTTPDDoS攻击的特性,增强了应用服务器的防护能力。

(3)负载均衡服务是ECS面向多机方案的一个配套服务,需要同ECS结合使用。

.负载均衡配置

1.首选准备三台服务器

分别是:

负载均衡调度器:192.168.1.208

WEB服务器1  :192.168.1.209

WEB服务器2  :192.168.1.197

2.将两个web服务器进行nginx网站部署

关于nginx部署web服务不多做介绍,需要的话详见:

WEB服务器1 :192.168.1.208

这边部署了豆瓣主页的网页:访问效果如下

 

 

 

WEB服务器2 :192.168.1.197

 

这边部署了斗鱼主页的网页:访问效果如下

 

 

 

3.对负责均衡调度器进行配置

配置如下:

#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 web_upstream{
       server 192.168.1.197;
       server 192.168.1.209;
       server www.maoyan.com;
    } 
    server {
        listen       80;
        server_name  localhost;
        
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://web_upstream;
            root   html;
            index  souhu.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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

其实就是这样:

http代码块里面,添加配置

 

upstream web_upstram {
    server 192.168.1.197;
    server 192.168.1.209;
}

 

location代码块内,添加

 

location / {

    proxy_pass http://web_upstram;

    root   html;

    index  index.html index.htm;

}

在上面多加了一个负载均衡www.maoyan.com

4.测试负责均衡调度器

对负责均衡调度器:192.168.1.208进行访问

(1)测试1

 

 

(2)测试2

 

 

 

(3)测试3 这个测试中,因为用的是真的官网猫眼,可能是猫眼设置了代理屏蔽等,所有出现页面不存在问题

 

 

 

以上说明,访问负载均衡调度器,负责均衡生效,这样在生产中,每台web服务器部署相同的web服务,如果一台服务器挂了,就不会造成影响线上服务了.

 

posted @ 2018-09-15 17:50  pycoder_hsz  阅读(391)  评论(0编辑  收藏  举报