1. Load balancing and Reverse proxy introduction
Load Balancing is a collection of servers that are symmetric in a single server, each of which can be serviced independently by load balancing technology that distributes client requests evenly to a server in the server collection, and the server responds to client requests independently, thus resolving high concurrency accesses.
Reverse proxy refers to the proxy server to accept the user request, and then forward the request to the server on the internal network, and the results from the server to return to the user, the proxy servers are external performance as a server, the proxy server does not save any Web page data, All static Web pages and CGI programs are stored on the Web server on the internal network, increasing the security of the Web service.
2. Common Load-balancing methods
2.1 User manual selection, common in the website where the business is downloaded.
2.2DNS polling, a domain name, can be resolved to multiple servers. However, the reliability is not high, once a server is down, the request to resolve to this server can not respond, and the DNS cache time is longer, even if the removal of DNS server resolution does not take effect immediately, another problem load imbalance, because it does not reflect the health and performance of the individual servers, At the same time, the DNS cache causes the parsing to be on the same server.
2.3 Four/seven-layer load balancing
This 47 layer refers to the OSI standard network model, the four layer is the transport layer, and the seven layer is the application layer.
Four-layer load balancing can be implemented using hardware such as: F5 big-IP, Citrix netscaler, and so on, expensive. But it can also be implemented using software, which represents the LVS developed by Dr. Zhangwensong.
Seven-layer load Balancing general use of HTTP reverse proxy, the representative is Nginx, Haproxy, and so on, here is the main nginx, because it can be well configured, at the same time can be polled, iphash, weights, urlhash and other ways to achieve load balancing, can also check the health status of the backend server.
3. The general configuration of an Nginx reverse proxy server
User Nginx nginx;worker_processes1; #一般与CPU数量相等error_log/var/log/nginx/Error.log crit; #错误日志记录路径, error type Crit: Record least error #error_log logs/error.log notice; #error_log logs/error.logInfo;p ID/var/run/nginx.pid; #主进程号位置worker_rlimit_nofile51200; #打开最多文件数events {use epoll; #使用的IO模型, this is the event-triggered worker_connections51200; #最大连接数}http {include mime.types; Default_type Application/octet-stream; Server_names_hash_bucket_size -; #内存中缓存服务器名的hash表大小 client_header_buffer_size 32k; #用户头信息的缓冲大小, if the user cookie is large, set the big point large_client_header_buffers432k; #缓冲头信息的数量, and size #log_format main'$remote _addr-$remote _user [$time _local] "$request"' # '$status $body _bytes_sent "$http _referer"' # '"$http _user_agent" "$http _x_forwarded_for "'; #access_log logs/Access.log Main; Sendfile on; #tcp_nopush on; #keepalive_timeout0; Keepalive_timeout $; Tcp_nodelay on; #禁用nagle算法, this algorithm does not work with C/condition of S fastcgi_connect_timeout -; #fastcgi进程连接超时时长 fastcgi_send_timeout -; #发送超时时长 Fastcgi_read_timeout -; #响应超时时长 fastcgi_buffer_size 64k; #缓冲头大小 fastcgi_buffers464k; #缓冲数量和大小, generally php/in the sitethe Java script produces a page size median of fastcgi_busy_buffers_size 128k; #默认fastcgi_buffer_size两倍 fastcgi_temp_file_write_size 128k; #缓存临时文件的大小默认fastcgi_buffer_size两倍gzipOn ; Gzip_min_lenth 1k; #最少压缩的数据大小 gzip_buffers416k; #缓冲大小 gzip_http_version1.1; #版本 Gzip_comp_level2; #压缩比 gzip_types Text/plain Application/x-javascript text/css application/XML; #压缩文件mime类型 gzip_vary on; #head头上加vary头, determine whether the proxy server supports compression client_max_body_size 300m; #最大请求单个文件大小 client_body_buffer_size 128k; #请求缓冲区大小 Proxy_connect_timeout -; #连接超时时间 Proxy_read_timeout -; #连接后响应超时时间 Proxy_send_timeout -; #连接后传送数据超时时间 Proxy_buffer_size 16k; #缓冲大小 proxy_buffers432k; #缓冲大小和数量 proxy_busy_buffers_size 64k; #系统繁忙时, the maximum space for buffer applications is proxy_temp_file_write_size 64k; #临时文件缓存大小 upstream Php_server_pool {server192.168.226.131: themax_fails=2fail_timeout=30s; #定义服务器组 Server192.168.226.132: themax_fails=2fail_timeout=30s; Ip_hash; #解决session绑定问题, but the load balancing effect is general. } Server {Listen the; server_name www.shiyan.com; Location/{proxy_next_upstream http_502 http_504 timeout invalid_header; #响应码为多少时, the request is sent to another server Proxy_pass http://Php_server_pool; #服务器组Proxy_set_header Host www.shiyan.com; #访问的域名 Proxy_set_header X-forwarded-For $remote _addr; #访问经过的代理服务器, and the client's really IP} access_log/var/log/nginx/Www.shiyan.com_access.log; }
Of course, through the upstream instruction and the server directive can fully implement the reverse proxy static and Dynamic Web page separation, by defining the location of the request file format, to access the static cache server and different server groups, there is no more said this aspect.
4.Nginx load-Balanced HTTP upstream module
4.1 ip--hash directive
Backend is a number of dynamic application server, the ability to define a client's request to the same server, can solve the session problem, but such methods such as weight loss effect, load balance is not good, can session sharing to replace it.
4.2 Server Directives
The primary is the name of the backend server, the parameter, the name can be the domain name, IP port. Parameter weigth weight, max_fails the number of failed requests to the backend server, the time-out after the Fail_timeout request failed, the down flag server is permanently offline, and the backup flag server is enabled when the non-backup server is all down or busy.
4.3 upstream directive
Define server groups, variables: the upstream server IP that $upstream _addr request, $upstream the answer status of the _status server, $upstream _response_time response time, $upstream _http_$ Headers any HTTP protocol header information.
Nginx HTTP load balancing and reverse proxy