In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
(1)。 Experimental environment
Youxi1 192.168.5.101 load balancer
Youxi2 192.168.5.102 Host 1
Youxi3 192.168.5.103 Host 2
(2) .Nginx load balancing strategy
Nginx's load balancer is used to select a server from the list of backend servers defined by the upstream template to receive requests from users. A basic upstream module is as follows:
Upstream [server group name] {server [IP address]: [port number]; server [IP address]: [port number];....}
After the upstream module is configured, reverse proxy the specified access to the server list in the following format:
Location ~. * ${index index.jsp index.html; proxy_pass http://[ server group name];}
Extension: location configuration rule for nginx: https://www.jb51.net/article/182472.htm
This completes the most basic load balancing, but it does not meet the actual needs. Currently, Nginx's upstream module supports six load balancing strategies (algorithms): polling (default), weight (weight), ip_hash (based on ip allocation), least_conn (minimum connection), fair (response time provided by third parties) and url_hash (allocation based on URL adopted by third parties).
1) polling
The most basic configuration method is the default load balancing policy of the upstream module. Each request is evenly distributed to different back-end servers in chronological order. The parameters are as follows:
Fail_timeout in conjunction with max_fails uses max_fails for the maximum number of failures in the time set by the fail_timeout parameter. If all requests from the server fail during this time, then the fail_time server is considered to be down, and the default is 10s (the interval between server attempts that are considered to be down? Backup marks the server as a standby server When the primary server stops, the request is sent to it where the down marking server is permanently down
Note: the server marked by 1.down will be removed automatically; 2. The default is polling; 3. This strategy is suitable for server configuration of stateless and short-block services.
2) weight
Weight mode, which specifies the probability of polling on the basis of the polling policy. It can also be considered that a new parameter of weight is added on the basis of polling, which specifies the probability of polling, with a value of number. The upstream module configuration template is as follows:
Upstream [server group name] {server [IP address]: [port number] weight=2; server [IP address]: [port number];....}
In this example, the default value of the server without the weight parameter is 1 gravity weight, which is proportional to the access ratio, the sum of all weight values is a loop unit, and the weight value of the server itself is the number of polls in the loop unit.
Note: 1. The higher the weight, the more requests are allocated; 2. This strategy can be used in combination with least_conn policy and iphash policy. This strategy is suitable for the situation where there is a large gap in the configuration of server hardware.
3) ip_hash
According to the ip allocation method, specify the load balancer according to the client IP-based allocation method, which ensures that the same client requests are consistently sent to the same server to ensure the session session. In this way, each visitor has regular access to a back-end server, which can solve the problem that session cannot cross servers. The upstream module configuration template is as follows:
Upstream [server group name] {ip_hash; server [IP address]: [port number] weight=2; server [IP address]: [port number];....}
Note: previous versions of 1.nginx1.3.1 cannot use weight in ip_hash; 2..ip_hash cannot be used with backup; 3. This strategy is suitable for programs with stateful services, such as session;4. When a server needs to be removed, it must be manually down off.
4) least_conn
The least connection method, sending the request to the back-end server with the least number of links. Polling distributes requests evenly among the backends so that their load is roughly the same. However, some requests take a long time and result in a high load on the backend where they are located. In this case, least_conn can achieve a better load balancing effect. The upstream module configuration template is as follows:
Upstream [server group name] {least_conn; server [IP address]: [port number] weight=2; server [IP address]: [port number];....}
Note: this strategy is suitable for server overload caused by different request processing times.
5) fair
According to the response time mode, the request is allocated according to the response time of the server, and the priority allocation of short response time. The upstream module configuration template is as follows:
Upstream [server group name] {server [IP address]: [port number] weight=2; server [IP address]: [port number];.... fair;}
Note: third-party plug-ins need to be installed.
6) url_hash
Url allocation method, which allocates requests according to the hash results of accessing url, so that each url is directed to the same backend server, which should be used in conjunction with cache hits. Multiple requests for the same resource may reach different servers, resulting in unnecessary multiple downloads, low cache hit rates, and waste of resource time. Using url_hash, you can make the same url (that is, the same resource request) reach the same server. Once the resource is cached and the request is received again, it can be read in the cache. The upstream module configuration template is as follows:
Upstream [server group name] {hash $request_uri; server [IP address]: [port number] weight=2; server [IP address]: [port number];....}
Note: 1. Third-party plug-ins need to be installed; 2.uri, which is I, is not lowercase L.
(3)。 Experiment
1) compile and install nginx on the load balancer youxi1
Install the dependency package for nginx
[root@youxi1 ~] # yum-y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
Upload nginx source code package nginx-1.14.1.tar.gz, decompress and install
[root@youxi1] # tar xf nginx-1.14.1.tar.gz-C / usr/local/src/ [root@youxi1 ~] # cd / usr/local/src/nginx-1.14.1/ [root@youxi1 nginx-1.14.1] #. / configure-- prefix=/usr/local/nginx-- with-http_dav_module-- with-http_stub_status_module-- with-http_addition_module-- with-http_sub_module-- with- Http_flv_module-- with-http_mp4_ module [root @ youxi1 nginx-1.14.1] # make & & make install [root@youxi1 nginx-1.14.1] # echo $? 0
Parameter description:
-- with-http_dav_module, enabling ngx_http_dav_module support (adding PUT,DELETE,MKCOL: create collections, COPY and MOVE methods) is off by default and needs to be compiled on
-- with-http_stub_status_module to enable ngx_http_stub_status_module support (get the working status of nginx since it was last started)
-- with-http_addition_module, enabling ngx_http_addition_module support (as an output filter, supports incomplete buffering, partial response to requests)
-- with-http_sub_module to enable ngx_http_sub_module support (allows some text in the nginx response to be replaced with some other text)
-- with-http_flv_module, enabling ngx_http_flv_module support (provides time-based offset files for seeking memory usage)
-- with-http_mp4_module, enabling support for mp4 files (provides time-based offset files for seeking memory usage).
Generate nginx users
[root@youxi1 nginx-1.14.1] # useradd-M-s / sbin/nologin nginx
Start and add self-boot
[root@youxi1 nginx-1.14.1] # / usr/local/nginx/sbin/nginx [root@youxi1 nginx-1.14.1] # echo / usr/local/nginx/sbin/nginx > > / etc/rc.local [root@youxi1 nginx-1.14.1] # chmod + x / etc/rc.d/rc.local
If the firewall is on, remember to add the port number
[root@youxi1 nginx-1.14.1] # firewall-cmd-permanent-zone=public-add-port=80/tcp & & firewall-cmd-reloadsuccesssuccess
Check the interface below to see if nginx is installed properly.
After the test, modify the configuration file of nginx, and finally restart nginx
[root@youxi1 nginx-1.14.1] # cp / usr/local/nginx/conf/nginx.conf {, .bak} [root@youxi1 nginx-1.14.1] # vim / usr/local/nginx/conf/nginx.confuser nginx; / / Line 2 location / {/ 43 root html; index index.html index.htm; if ($request_uri ~ *\ .html $) {proxy_pass http://htmlservers; } if ($request_uri ~ *\ .php $) {proxy_pass http://phpservers;} proxy_pass http://picservers;}upstream htmlservers {/ / under the http module, add server 192.168.5.102 server 80 at the level of the server module; server 192.168.5.103 server 80;} upstream phpservers {server 192.168.5.103 proxy_pass 80; server 192.168.5.103 80 } upstream picservers {server 192.168.5.102 usr/local/nginx/sbin/nginx 80; server 192.168.5.103 server 80;} [root@youxi1 nginx-1.14.1] # / usr/local/nginx/sbin/nginx-s reload
2) arrange web programs on youxi2 and youxi3
[root@youxi2 ~] # yum-y install httpd [root@youxi2 ~] # echo youxi2 > / var/www/html/index.html [root@youxi2 ~] # echo youxi2.php > / var/www/html/index.php [root@youxi2 ~] # echo youxi2.other > / var/www/html/index.jsp [root@youxi2 ~] # systemctl start httpd.service [root@youxi3 ~] # yum-y install httpd [root@youxi3 ~] # echo youxi3 > / var/www/html/index.html [root@youxi3 ~] # echo youxi3.php > / var/www/html/index.php [root@youxi3 ~] # echo youxi3.other > / var/www/html/index.jsp [root@youxi3 ~] # systemctl start httpd.service
If the firewall is on, remember to add the port number
[root@youxi2] # firewall-cmd-- permanent-- zone=public-- add-port=80/tcp & & firewall-cmd-- reloadsuccesssuccess [root@youxi3] # firewall-cmd-- permanent-- zone=public-- add-port=80/tcp & & firewall-cmd-- reloadsuccesssuccess
3) Test
First test the html page
Then test the php page
Finally, test the other pages
Reference: https://www.cnblogs.com/1214804270hacker/p/9325150.html
This is the end of this article on the implementation of Nginx load balancing cluster. For more information about Nginx load balancing cluster, please search the previous articles or continue to browse the relevant articles below. I hope you will support it in the future!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.