Nginx 高階篇(三)負載均衡的實現

huxiaobai_001發表於2020-03-18

一聽負載均衡感覺老牛逼了,反正我第一次聽的時候感覺特別高大上牛逼哄哄的樣子!後來隨著對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 協議》,轉載必須註明作者和本文連結

胡軍

相關文章