1-- [high-performance Nginx server] --7 Load Balancing

1 load balancing role

Address the high concurrency issues

Load balancing: builds on existing network architecture, it provides a cheap and effective and transparent method 扩展网络设备, and 服务器的带宽, 增加吞吐量, 加强网络数据处理能力, 提高网络的灵活性和可用性.

Load balancing, the English name for the Load Balance, which means that spread over multiple operating units to perform, such as: Web server, FTP server, enterprise critical application servers and other mission-critical servers, so as to work together to complete the task.

Load balancing is, all requests First load balancer, the load balancing algorithm in use by the load balancer ( 轮询, , IP绑定) 权重of the actual distributed to different servers, which is the object of the server cluster, a cluster of individual servers is to reduce the pressure .

Here Insert Picture Description

FIG 2 Network Model

The OSI layer Features TCP / IP protocol suite.
Application layer File transfer, e-mail, file services, virtual terminal TFTP、HTTP、SNMP、FTP、SMTP、DNS、RIP、Telnet
Presentation layer Data formatting, code conversion, data encryption No agreement
Session Layer Lift or establish contact with other contacts No agreement
传输层 提供端对端的接口 TCPUDP
Network layer Routing the data packet IP、ICMP、OSPF、BGP、IGMP、ARP、RARP
data link layer And a transmission frame has an error detection function address SLIP、CSLIP、PPP、MTU、ARP、RARP
Physical Layer In the form of binary data and then transmit data on physical media ISO2110、IEEE802、IEEE802.2

The difference between four and seven 3 load balancing

四层负载均衡: The network model 传输层based on mainly based TCPpackets to implement load balancing (for example LVS, haproxythat four load balancer), source address and destination address of the packet is rewritten.

七层负载均衡: In the network model 应用层based on URLor HTTPprotocols to achieve load balancing, Web server.

Nginx after 1.9, also supports 四层负载均衡the

Shortcoming 4 load balancing

After using load balancing, server clusters are actually used, then this time will produce many distributed related issues.

such as:

  • Distributed Sessionconsistency
  • Distributed timed task scheduling 幂等性problem
  • Distributed generation 全局 ID
  • 分布式锁 solution
  • Distributed Configuration Center

5 Nginxconfigure load balancing

NginxLoad balancing provides upstream server (real business logic accessible server), 负载均衡, 故障转移, 失败重试, 容错, 健康检查and so on.

When the upstream server (real-world business logic to access a server) fails, the other can be transferred to the upstream server (real-world business logic to access the server).

Nginx is best not to use the 失败重试mechanism to produce power and other issues, there are micro-service retry mechanism.

5.1 Upstream ServerLoad Balancing

上游服务器: Using 负载均衡器forwarded to the real business server

Upstream ServerChinese translation 上游服务器, means that load balancing server settings, vernacular representation (nginx proxy server is to be the last real visit).

负载均衡算法: Configure multiple upstream server (real business logic accessible server) load balancing mechanism.

失败重试机制: When the upstream server (real business logic accessible server) timeout or the server does not survive, consider whether to retry mechanism (compensation mechanism).

服务器心跳检测: When the upstream server (real business logic accessible server), Monitoring Detection | heartbeat.

5.2 Upstream ServerConfiguration

###定义上游服务器(需要被nginx真实代理访问的服务器) 默认是轮询机制
upstream test{
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
}

server {
    listen       80;
    server_name  www.test.com;
	
    location / {
        proxy_pass http://test;
        index  index.html index.htm;
    }

}

Refresh Nginx:

/usr/local/nginx/sbin/nginx -s reload

test:
Here Insert Picture Description
Here Insert Picture Description

6 load balancing algorithms

1, the polling (default)

Each request individually assigned to a different time order back-end services, if a backend server crashes, the system automatically remove the fault, so that the user access is not affected.

2, weight (weight polling)

weightThe higher the value the greater the probability assigned to visit, mainly used for each back-end server performance unbalanced situation. Or using only the resources of the host to set different weights in the case of the master, to achieve reasonable and effective manner.

3、ip_hash

Each request is assigned according to a hash result Access IP, so that visitors from the same IP fixed access a back-end servers, and can effectively solve the problems of dynamic web sessionsharing. Commonly known as IP binding.

4, fair(third party)

Than weight, ip_hashmore intelligent load balancing algorithm, fairthe algorithm can be based on the page size and load duration intelligently load balancing, which is based on an allocation request to the backend server response time, short response time priority allocation. NginxIt does not support itself fair, if this scheduling algorithm needs, you must install the upstream_fairmodule.

5, url_hash(third party)

Accessed by URLthe hash result allocation request, each URLdirected to a back-end servers, back-end efficiency can be further improved cache server. NginxItself does not support url_hash, if needed this scheduling algorithm, you must install Nginxthe hashpackage.

6.1 Polling (default)

Each request individually assigned to a different time order back-end services, if a backend server crashes, the system automatically remove the fault, so that the user access is not affected.

6.2 Weight Weight

Server can be adjusted according to the actual weight of the server. The more the higher the weight assigned to the request, the lower the weight, the less the request. Default is 1.

###定义上游服务器(需要被nginx真实代理访问的服务器) 默认是轮询机制
upstream test{
    server 127.0.0.1:8001 weight=1;
    server 127.0.0.1:8002 weight=2;
}

server {
    listen       80;
    server_name  www.test.com;
	
    location / {
        proxy_pass http://test;
        index  index.html index.htm;
    }

}

6.3 IP Binding ip_hash

Each access request is assigned by the IP hash result of the visitor from the same IP-based fixed access to a back-end server, and 可以有效解决动态网页存在的 session 共享问题. Commonly known as IP binding.

###定义上游服务器(需要被nginx真实代理访问的服务器) 默认是轮询机制
upstream test{
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
    ip_hash;
}

server {
    listen       80;
    server_name  www.test.com;
	
    location / {
        proxy_pass http://test;
        index  index.html index.htm;
    }

}

7 Nginxconfigure failover

When the upstream server (real access to the server), once the failure is not timely or appropriate, it should be in rotation directly to the next server to ensure high availability server.

Nginx Configuration code:

###定义上游服务器(需要被nginx真实代理访问的服务器) 默认是轮询机制
upstream test{
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
}

server {
    listen       80;
    server_name  www.test.com;
    location / {
        ### 指定上游服务器负载均衡服务器
        proxy_pass http://test;
        ### nginx与上游服务器(真实访问的服务器)超时时间 后端服务器连接的超时时间_发起握手等候响应超时时间
        proxy_connect_timeout 1s;
        ### nginx发送给上游服务器(真实访问的服务器)超时时间
        proxy_send_timeout 1s;
        ### nginx接受上游服务器(真实访问的服务器)超时时间
        proxy_read_timeout 1s;
        index  index.html index.htm;
    }
}

8 Nginx rewrite

NginxGlobal variables provide variable or set up their own, combined with regular expressions and flags achieve urlrewriting and redirection. rewriteOnly on the server{}, location{}, if{}, and act only outside the string parameter passed behind the removed name.

RewriteThe main function is to achieve URLrewrite Nginxthe Rewriterules adopted Pcre, perlcompatible regular expression syntax rules to match, if you need Nginxa Rewritefunction, the compiler Nginxbefore, we need to compile and install PCRElibraries.

By Rewritethe rules, norms can be achieved URL, according to do variable URLsteering configuration and selection.

8.1 RewriteGlobal Variables

nginx rewrite rule is to use the regular matching request url, then rewrite and changed according to defined rules required ngx_http_rewrite_modulemodules to support urlrewriting, the modules are standard modules, installed by default.

variable meaning
$args This variable is equal to the parameter request line, with $query_string
$content length Request header Content-lengthfield
$content_type Request header Content-Typefield
$document_root Value specified in the current request instruction root
$host Host request header field, otherwise the server name
$http_user_agent Client agentInformation
$http_cookie Client cookieInformation
$limit_rate This variable can limit connection speed
$request_method Operation of client requests, generally GETorPOST
$remote_addr Client IPaddress
$remote_port Client port
$remote_user After already Auth Basic Moduleauthenticated user name
$request_filename The file path of the current request, or a request by the root URI generation instruction alias
$scheme HTTPThe method (e.g. http, https)
$server_protocol Protocol requests, usually HTTP/1.0orHTTP/1.1
$server_addr Server address, after the completion of a system call can determine this value
$server_name name of server
$server_port Request reaches the server port number
$request_uri Original parameters from the request URIdoes not include a host name, such as/foo/bar.php?arg=baz
$uri With no current request parameter URI, $uriit does not include a host name, such as/foo/bar.html
$document_uri The $urisame

8.2 determine the IPaddress of the source

## 如果访问的ip地址为192.168.5.165,则返回403
if  ($remote_addr = 192.168.5.166) {  
    return 403;  
}  

8.3 restrict browser access

## 不允许谷歌浏览器访问 如果是谷歌浏览器返回500
if ($http_user_agent ~ Chrome) {   
    return 500;  
}
Published 675 original articles · won praise 214 · Views 140,000 +

Guess you like

Origin blog.csdn.net/weixin_42112635/article/details/104950900
Recommended