当前位置:首页 > 服务器技术 > nginx

Nginx 之负载均衡配置

负载均衡服务器策略:

1.轮循? 每个请求逐个分发到后端服务器

2.加权轮循? 按照分配的权重将请求分发到后端服务器

3.ip hash 轮询的基础上,保持一个客户端多次请求分发到一台后端服务器上

一、轮询配置

#定义后端服务器组
    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;
        }
        
    }

二、轮询加权配置

#定义后端服务器组
    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;
        }
    }

三、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;
        }    
    }

 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!