Linux, nginx + nodejs multiple server load balancing

1. Preparation: 1, closed Selinux 2, load balancing Category:

2, open the firewall corresponding port

1) One is performed by hardware solutions, common hardware NetScaler, F5, Radware, and Array and other commercial load balancer, but they are more expensive
2) one is to be solved by software, common software there LVS, Nginx, apache, etc., they are based on Linux and open source system load balancing strategy.

Nginx is characterized by possession of less memory, high concurrency, the ability to do concurrent nginx fact the same type of performance the best of the web server, mainland China use nginx web site users are: Sina, Netease, Tencent and so on.

nginx's upstream distribution currently supports 3 ways:

1), polling (default) each request individually assigned to a different time order back-end server, if the back-end server is down, can be automatically removed.

2)、weight 权重 ——you can you up

Polling a probability proportional to weight ratio and access, for the case where unevenness backend server performance.

3), ip_hash ip hash algorithm

Each request is assigned according ip hash result of the visit, so that each visitor to access a fixed back-end server, can solve the problem of session.

Configure load balancing

 

Find /etc/nginx/conf.d then create a new site in the corresponding configuration file inside

 

{bakeaaa upstream 
    
    ip_hash; 
    Server 127.0.0.1:3001. 1 = weight ; 
    Server 127.0.0.1:3002. 1 = weight ; 
    Server 192.168.1.129:3001 weight =. 3 ; 
} 



Server { 
    the listen        80 ; 
    server_name www.aaa.com; 

    #charset KOI8 - R & lt; 
    #access_log   / var / log / Nginx / host.access.log main; 

    LOCATION / { 
    # set the host real address header and the client, the server acquires the client transactions to the IP 
    proxy_set_header the host host $; 
    proxy_set_header X- -Real- $ REMOTE_ADDR IP; 
    proxy_set_header the X--Forwarded-For $proxy_add_x_forwarded_for;
    #禁用缓存
    proxy_buffering off;
        
    proxy_pass http://bakeaaa;
    }
    location /socket.io {        
        # 此处改为 socket.io 后端的 ip 和端口即可
        proxy_pass http://127.0.0.1:3001;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }

    #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   /usr/share/nginx/html;
    }
 
}

Restart

Restart 
nginx systemctl restart nginx 
nginx - t see configured correctly 
systemctl STOP nginx 
systemctl Start nginx

Firewall Configuration

Add: 
Firewall -cmd = --zone public --add-Port = 80 / TCP - Permanent 
reload: 
Firewall -cmd --reload

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11655079.html