Nginx 高级篇(三)负载均衡的实现

一听负载均衡感觉老牛逼了,反正我第一次听的时候感觉特别高大上牛逼哄哄的样子!后来随着对nginx服务器的不断深入掌握才发现,其实没那么难那么高大上!

首先你得了解什么是”负载均衡”?
详情参考我的另一篇博客:博客:Nginx 高级篇(二)什么是负载均衡详情参考我的另一篇博客:博客:Nginx 高级篇(二)什么是负载均衡

我们进入实战,拿tp框架做个小案例,访问内网10.10.16.170:80 然后利用负载均衡,将访问分发到10.10.16.170:81 和 10.10.16.170:82 上去 当然这里只是举例子,实战当中81和82的站点你可以部署到不同的服务器上去 我没这么多服务器 就连nginx apache都搞在了一台服务器上 因为我懒啊!
重点看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;
    upstream tpserver {
        server 10.10.16.170:81 weight=1 max_fails=2 fail_timeout=3;
        server 10.10.16.170:82 weight=1 max_fails=2 fail_timeout=3;
    }
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/tp5/public;
            index  index.php index.html index.htm;
        if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=/$1  last;
        }
        }

        #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$ {
          proxy_pass http://tpserver;
          # proxy_pass http://10.10.16.170:8080;
          # root html/tp5/public;
          # fastcgi_pass   127.0.0.1:9000;
          # fastcgi_index  index.php;
          # fastcgi_param  SCRIPT_FILENAME  $document_root$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;
        #}
    }



     server {

        listen       81;
        server_name  localhost;

        location / {
            root   html/tp51/public;
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
            }
        }

        location ~ \.php$ {
           #proxy_pass http://www.baidu.com;
           root html/tp51/public;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }

        access_log logs/51-access.log;

    }

    server {

        listen       82;
        server_name  localhost;

        location / {
            root   html/tp52/public;
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
            }
        }

        location ~ \.php$ {
           root html/tp52/public;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }

    access_log logs/52-access.log;

    }

    # 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;
    #    }
    #}

}

80站点正常配置,当请求的是php文件的时候 我们通过proxy_pass代理到多台服务器那里去 ,
但是如何通过proxy_pass指向多台服务器呢?
把多台服务器用upstream指定绑定在一起并起一个组名

 upstream tpserver {
        server 10.10.16.170:81 weight=1 max_fails=2 fail_timeout=3;
        server 10.10.16.170:82 weight=1 max_fails=2 fail_timeout=3;
    }

weight = 1表示权重 自己看: https://blog.csdn.net/zhangskd/article/det...
max_fails = 2 表示链接失败2次就放弃该服务器
fail_timeout = 3 表示连接超时3sfail_timeout = 3 表示连接超时3s

然后通过proxy_pass指向该组即可:

proxy_pass http://tpserver;

这样你访问10.10.16.170的时候就会将请求转发到不同的服务器(站点)上去访问 当然是根据你所分配的权重去分配的!详情去参考权重的介绍:https://blog.csdn.net/zhangskd/article/det...
另外你也可以了解一下nginx的负载均衡内部算法是如何实现的,请自行Google,Google比我所的更好!
可以去看81和82的访问日志 一次是访问的81 一次是访问的82 将流量分摊到了不同的服务器站点运行!
你可以对整个网站进行负载均衡的部署 也可以针对流量较大的接口进行负载均衡的部署 负载均衡是一种解决大量流高并发的很好的解决方案!

本作品采用《CC 协议》,转载必须注明作者和本文链接
胡军
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!