Nginx 之負載均衡與反向代理

負載均衡服務器策略:php

1.輪循  每一個請求逐個分發到後端服務器html

2.加權輪循  按照分配的權重將請求分發到後端服務器 前端

3.ip hash 輪詢的基礎上,保持一個客戶端屢次請求分發到一臺後端服務器上nginx

1、輪詢配置後端

  #定義後端服務器組
    upstream nginx-test{
        server 192.168.0.128;
        server 192.168.0.127;
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
        location / {
           index  index.html index.htm index.php l.php;
           autoindex  on;
           proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location ~ \.php(.*)$  {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
        
    }

2、輪詢加權配置服務器

  #定義後端服務器組
    upstream nginx-test{
        server 192.168.0.128 weight=2;
        server 192.168.0.127;
        server 192.168.0.126 backup; # 備份服務器,其餘服務器宕機後啓動
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
        location / {
           index  index.html index.htm index.php l.php;
           autoindex  on;
           proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

3、IP Hash配置負載均衡

  #定義後端服務器組
    upstream nginx-test{
        ip_hash;
        server 192.168.0.128;
        server 192.168.0.127;
        server 192.168.0.126;
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
        location / {
           index  index.html index.htm index.php l.php;
           autoindex  on;
           proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }    
    }

 4、負載均衡與反向代理區別spa

一、負載均衡須要經過反向代理來實現
二、反向代理就是指nginx做爲前端服務器,將請求轉發到後端,再將後端服務器的結果,返回給客戶端
它在中間作了一個代理服務器的角色
三、負載均衡對反向代理增長了一些策略,由於後端是多臺服務器,nginx會根據設定的策略將請求轉發給一個相對空閒的服務器,對負載進行分流,減輕服務器壓力代理

5、反向代理配置code

  #定義後端服務器組
    upstream nginx-test{
        server 192.168.0.127;
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
      #代理配置參數
        proxy_connect_timeout 180;
        proxy_send_timeout 180;
        proxy_read_timeout 180;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarder-For $remote_addr;
        
        location / {
          proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
相關文章
相關標籤/搜索