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 .
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 |
传输层 |
提供端对端的接口 |
TCP 、UDP |
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 TCP
packets to implement load balancing (for example LVS
, haproxy
that four load balancer), source address and destination address of the packet is rewritten.
七层负载均衡
: In the network model 应用层
based on URL
or HTTP
protocols 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
Session
consistency - Distributed timed task scheduling
幂等性
problem - Distributed generation
全局 ID
分布式锁
solution- Distributed Configuration Center
5 Nginx
configure load balancing
Nginx
Load 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 Server
Load Balancing
上游服务器
: Using负载均衡器
forwarded to the real business server
Upstream Server
Chinese 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 Server
Configuration
###定义上游服务器(需要被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:
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)
weight
The 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 session
sharing. Commonly known as IP binding.
4,
fair
(third party)
Than weight
, ip_hash
more intelligent load balancing algorithm, fair
the 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. Nginx
It does not support itself fair
, if this scheduling algorithm needs, you must install the upstream_fair
module.
5,
url_hash
(third party)
Accessed by URL
the hash result allocation request, each URL
directed to a back-end servers, back-end efficiency can be further improved cache server. Nginx
Itself does not support url_hash
, if needed this scheduling algorithm, you must install Nginx
the hash
package.
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 Nginx
configure 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
Nginx
Global variables provide variable or set up their own, combined with regular expressions and flags achieve url
rewriting and redirection. rewrite
Only on the server{}
, location{}
, if{}
, and act only outside the string parameter passed behind the removed name.
Rewrite
The main function is to achieve URL
rewrite Nginx
the Rewrite
rules adopted Pcre
, perl
compatible regular expression syntax rules to match, if you need Nginx
a Rewrite
function, the compiler Nginx
before, we need to compile and install PCRE
libraries.
By Rewrite
the rules, norms can be achieved URL
, according to do variable URL
steering configuration and selection.
8.1 Rewrite
Global Variables
nginx rewrite rule is to use the regular matching request url, then rewrite and changed according to defined rules required ngx_http_rewrite_module
modules to support url
rewriting, 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-length field |
$content_type |
Request header Content-Type field |
$document_root |
Value specified in the current request instruction root |
$host |
Host request header field, otherwise the server name |
$http_user_agent |
Client agent Information |
$http_cookie |
Client cookie Information |
$limit_rate |
This variable can limit connection speed |
$request_method |
Operation of client requests, generally GET orPOST |
$remote_addr |
Client IP address |
$remote_port |
Client port |
$remote_user |
After already Auth Basic Module authenticated user name |
$request_filename |
The file path of the current request, or a request by the root URI generation instruction alias |
$scheme |
HTTP The method (e.g. http , https ) |
$server_protocol |
Protocol requests, usually HTTP/1.0 orHTTP/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 URI does not include a host name, such as/foo/bar.php?arg=baz |
$uri |
With no current request parameter URI , $uri it does not include a host name, such as/foo/bar.html |
$document_uri |
The $uri same |
8.2 determine the IP
address 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;
}