In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Many novices are not very clear about how to deploy http-based load balancers in nginx. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Nginx load balancing across multiple application instances is a common technique used to optimize resource utilization, maximize throughput, reduce latency, and ensure fault-tolerant configuration.
Environment introduction
Configure nginx load balancer because multiple servers will be used for it, so I will use docker below. For specific docker, please move to docker practice.
System environment: root@ubuntu:~# lsb_release-a # View system version No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 19.10 Release: 19.10 Codename: eoan root@ubuntu:~# uname-a # check how many bits the system is Linux ubuntu 5.3.0-18-generic # 19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86'64'64 x86'64 GNU/Linuxdocker version and related operating system version docker version: root@ubuntu:~# docker-- version # View docker version Docker version 19.03.3 Build a872fc2f86 operating system version: [root@57b669db1de1 /] # cat / etc/redhat-release # View system version CentOS Linux release 8.0.1905 (Core) [root@57b669db1de1 /] # uname-a Linux 57b669db1de1 5.3.0-18-generic # 19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86 "64 GNU/Linux Software version: nginx version: root@ubuntu:~# nginx-v View nginx version nginx version: nginx/1.16.1 (Ubuntu) docker nginx version: [root@57b669db1de1 /] # nginx-v # View nginx version nginx version: nginx/1.14.1 installation nginx
For specific nginx installation, please refer to nginx installation and deployment.
Install dependent software update package: root@ubuntu:~# apt-get update install dependent software: root@ubuntu:~# apt- y install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make gcc compile install nginx download nginx: root@ubuntu:~# wget http:#nginx.org/download/nginx-1.17.6.tar.gz extract: root@ubuntu:/opt# tar zxf nginx-1.17.6.tar.gz root@ubuntu:/opt# Cd nginx-1.17.6/ root@ubuntu:/opt/nginx-1.17.6# ls # lists the directory auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src root@ubuntu:/opt/nginx-1.17.6# configuration: root@ubuntu:/opt/nginx-1.17.6#. / configure-- prefix=/usr/local/nginx compilation and deployment: root@ubuntu:/opt/nginx-1.17.6# make & & make install root@ubuntu:/opt/nginx-1.17.6# cd / usr/local/nginx/ root@ubuntu:/usr/local/nginx# ls conf html logs sbin launch: root@ubuntu:/usr/local/nginx# ln sbin/nginx / usr/local/sbin/ # create nginx soft connection Set to command root@ubuntu:/usr/local/nginx# nginx# to start nginx root@ubuntu:/usr/local/nginx# netstat-anpl | grep nginx# to check whether the nginx port is open root@ubuntu:/usr/local/nginx# lsof-ianpl 80 # check whether port 80 is enabled docker install nginx create container download image: root@ubuntu:~# docker pull registry.cn -beijing.aliyuncs.com/blxt/centos create container and launch: root@ubuntu:~# root@ubuntu:~# docker run-itd-- name nginx1-- privileged registry.cn-beijing.aliyuncs.com/blxt/centos / sbin/init root@ubuntu:~# docker ps-a # check whether the container is created and start CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAME 6801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/ sbin/init" 5 minutes ago Up 5 minutes nginx1 creates a second container in the same way: root@ubuntu:~# docker run-itd-- name nginx2-- privileged registry.cn -beijing.aliyuncs.com/blxt/centos / sbin/init root@ubuntu:~# docker ps-a root@ubuntu:~# root@ubuntu:~# docker ps-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d31d3fc0ec1 registry.cn-beijing .aliyuncs.com / blxt/centos "/ sbin/init" 4 minutes ago Up 4 minutes nginx2 6801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/ sbin/init" 5 minutes ago Up 5 minutes nginx1 connect to the container: root@ubuntu:~# docker exec-it nginx1 / bin/bash root@ubuntu: ~ # docker exec-it nginx2 / bin/bash [root@6801d55682a3 /] # hostname nginx1 # change the host name [root@6801d55682a3 /] # bash # to make the changed name effective [root@1d31d3fc0ec1 /] # hostname nginx2 [root@1d31d3fc0ec1 /] # bash install the nginx update package: [root@nginx1 /] # yum clean all # clear the cache [root@nginx1 /] # yum makecache # update package install nginx: [root@nginx1 /] # yum-y install nginx [root@nginx1 /] # rpm-qa | grep nginx # check whether nginx has been installed and started: [root@nginx1 /] # systemctl start nginx [root@nginx1 /] # netstat-anpl | grep nginx # check whether the nginxnn port is open [root@nginx1 /] # lsof -i 80 # check whether port 80 is open and install the second nginx using the same method! Omitted here! Visit nginx: [root@nginx1 /] # curl 127.0.0.1 modify web page content to view web page existence directory: [root@nginx1 nginx] # more nginx.conf # View configuration file find the following root line in the configuration file, and the following directory is the web page file storage directory root / usr/share/nginx/html [root@nginx1 html] # cd / usr/share/nginx/html/ [root@nginx1 html] # ls # list directory contents 404.html 50x.html index.html nginx-logo.png poweredby.png [root@nginx1 html] # echo nginx1 > index.html # clear the contents of the nginx home page file and rewrite the contents as nginx1 nginx2 Be careful not to set the same homepage content to access nginx: [root@nginx1 html] # curl 127.0.0.1 nginx1 [root@nginx2 ~] # curl 127.0.0.1 nginx2 modify configuration file nginx1 modify content as follows: server {listen 80 Server_name www.nginx1.com; / / set domain name # charset koi8-r; # access_log logs/host.access.log main; location / {root html; index index.html index.htm;}} nginx2 modified as follows: server {listen 80; server_name www.nginx2.com; / / set domain name # charset koi8-r # access_log logs/host.access.log main; location / {root html; index index.html index.htm;}} configure http load balancer
To start load balancing HTTP traffic for a group of servers using NGINX Plus or NGINX open source, define the group using the upstream directive, which is placed in the http context, and the proxy_pass directive is used to forward requests to the backend, which is generally specified in the loction context.
Basic configuration method:
Http {upstream nginx {# test is the group name server www.nginx1.com; # server is used to specify the access address of the backend server. Generally, you can specify the domain name, ip, port, domain name and IP. The port can ignore server www.nginx2.com weight=5; # weight to specify the weight value server www.nginx3.com down. # down is used to stop forwarding to this server. Omit other configurations} server {location / {proxy_pass http://nginx; # http://nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}}
Nginx officially provides four methods for load balancing. These four methods are described one by one:
Round Robin
Requests are distributed evenly among servers, taking into account server weights. This method is used by default (there is no instruction to enable it)
The default configuration is Round Robin, and the configuration method:
Http {upstream nginx {# test is the group name server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http://nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}} minimum connections
The method of sending requests with the least active connections to the server is also set up taking into account the weight of the server
Configuration method:
Http {upstream nginx {# test is the group name less_conn; server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http:#nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}} ip_hash
Determine the server to which the request is sent from the client IP address. In this case, the hash value can be calculated using the first three octets of the IPv4 address or the entire IPv6 address. This method ensures that requests from the same address will arrive at the same server unless it is unavailable.
Configuration method:
Http {upstream nginx {# test is the group name ip_hash; server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http:#nginx is the back-end server that forwards that group. Omit other configurations}. Omit other configurations}} hash
The server to which the request is sent is based on a user-defined key, which can be a text string, variable, or combination.
Configuration method:
Http {upstream nginx {# test is the group name hash $request_uri consistent; server www.nginx1.com; # server is used to specify the access address of the backend server server www.nginx2.com; # weight specifies the weight value. Omit other configurations} server {location / {proxy_pass http://nginx; # http://nginx is the back-end server that forwards that group. Omit other configurations}. Omitting other configurations}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.