Nginx Load Balancing

Source: Internet
Author: User
Tags nginx load balancing

5 scheduling algorithms for Nginx load balancing:
1) Polling (default): Each request according to the order of request to a different back-end server, if the back end of a server down, nginx because it has a back-end health check function, so can automatically eliminate;
2) Weight: Weighted polling, weight is proportional to the access ratio, mainly for the performance of the backend server is not uniform;
3) Ip_hash: Each request is allocated according to the hash result of the access IP; When the request arrives, the IP hashes a value through the hash algorithm, and in a subsequent request, the IP hash value is assigned to the same server as long as it is identical. Effectively solve the existence of dynamic Web pages of the session sharing problem;
4) Fair (third party): According to the response time of the backend server to allocate the request, the response time is short priority allocation, if using this scheduling algorithm, must download nginx third-party module Upstream_fair;
5) Url_hash (third party): According to the URL of the access hash result allocation request, the same URL directed to the same back-end server, applicable to the backend server cache, provide the cache efficiency of the backend server;

Status parameters supported in upstream:
In the HTTP upstream module, you can specify the IP address and port of the backend server through the server directives, and you can also set the state of each back-end server in the load scheduling policy; Common states are:
1) Down: Indicates that the server is temporarily not participating in load balancing;
2) Backup: Back Up server, when other non-backup server fails, will request Backuo server, generally this machine pressure is the lightest;
3) Max_fails: The number of times the request failed is allowed, the default is 1, and when the maximum number of times is exceeded, the error defined by the Proxy_next_upstream module is returned;
4) Fail_timeout: After the max_fails failure, the time of the service is suspended, and the general and Max_ fails are used together;

Client Host: 172.25.16.3

Back-end Load Balancer Host: 172.25.16.2

172.25.16.4
1. Installing dependent Packages
[email protected] ~]# yum install-y gcc-c++ make bison openssl-devel zlib-devel cmake pcre-devel

2. Decompression, source code compilation and installation
[Email protected] ~]# TAR-ZXVF nginx-1.6.3.tar.gz
[Email protected] ~]# CD nginx-1.6.3
[Email protected] nginx-1.6.3]#/configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ Ssl_module
[[email protected] nginx]# make && make install
[Email protected] nginx]# ln-s/usr/local/nginx/sbin/nginx/usr/local/sbin/

3. Add the System user and specify the directory
[Email protected] nginx]# useradd-d/usr/local/nginx/-m-s/sbin/nologin nginx

4. Modify the configuration file
[Email protected] nginx]# vim/usr/local/nginx/conf/nginx.conf
User Nginx Nginx;
HTTP {
Upstream Westos {#负载均衡集群
Server 172.25.16.2;
Server 172.25.16.4;
}
Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
server {
Listen 80;
server_name localhost;
Location/{
Proxy_pass Http://westos;
}
}
}
#注: Upstream is defined outside the server{}, inside the http{}. After you define it, use the Proxy_pass reference.
[Email protected] nginx]# Nginx #开启nginx
[Email protected] nginx]# nginx-t #检查配置文件是否有语法错误
[Email protected] nginx]# nginx-s Reload #重新加载nginx
#此时从客户端刷新浏览器, WEB2 and web4 alternately appear
#几种调度策略的书写方式

Scheduling strategy for Weight
Upstream Westos {
Server 172.25.16.2 weight=3;
Server 172.25.16.4 weight=1;
}
Scheduling strategy for Ip_hash
Upstream Westos {
Ip_hash;
Server 172.25.16.2;
Server 172.25.16.4;
}
Perform a health check while configuring backup server to prevent all backend server failures from causing user inconvenience
Upstream Westos {
Server 172.25.16.2 max_fails=2 fail_timeout=2;
Server 172.25.16.4 max_fails=2 fail_timeout=2;
Server 172.25.16.5 backup;
}

This article is from the "8397752" blog, please be sure to keep this source http://8407752.blog.51cto.com/8397752/1686683

Nginx Load Balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.