current position:Home>How to configure load balancing for TCP in nginx server
How to configure load balancing for TCP in nginx server
2022-04-29 19:26:39【Billion cloud speed】
stay Nginx How to configure the server for TCP Load balancing of
Today, I'd like to share with you about Nginx The server How to configure for TCP Of Load balancing Relevant knowledge points of , Detailed content , Clear logic , I believe most people still know too much about this knowledge , So share this article for your reference , I hope you will gain something after reading this article , Now let's take a look .
One 、 install nginx
1. download nginx
# wget http://nginx.org/download/nginx-1.2.4.tar.gz
2. download tcp Module patch
# wget https://github.com/yaoweibin/nginx_tcp_proxy_module/tarball/master
Source page : https://github.com/yaoweibin/nginx_tcp_proxy_module
3. install nginx
# tar xvf nginx-1.2.4.tar.gz# tar xvf yaoweibin-nginx_tcp_proxy_module-v0.4-45-ga40c99a.tar.gz# cd nginx-1.2.4# patch -p1 < ../yaoweibin-nginx_tcp_proxy_module-a40c99a/tcp.patch#./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.30 --add-module=../yaoweibin-nginx_tcp_proxy_module-ae321fd/# make# make install
Two 、 Modify the configuration file
modify nginx.conf The configuration file
# cd /usr/local/nginx/conf# vim nginx.conf
worker_processes 1;events {worker_connections 1024;}tcp {upstream mssql {server 10.0.1.201:1433;server 10.0.1.202:1433;check interval=3000 rise=2 fall=5 timeout=1000;}server {listen 1433;server_name 10.0.1.212;proxy_pass mssql;}}
3、 ... and 、 start-up nginx
# cd /usr/local/nginx/sbin/# ./nginx
see 1433 port :
#lsof :1433
Four 、 test
# telnet 10.0.1.201 1433
5、 ... and 、 Use sql server client Tool testing
6、 ... and 、tcp How load balancing works
When nginx When a new client link is received from the listening port , Execute the routing algorithm immediately , Get the specified services that need to be connected ip, Then create a new upstream connection , Connect to the specified server .
tcp Load balancing support nginx The original scheduling algorithm , Include round robin( Default , Polling scheduling ), Hash ( Choose the same ) etc. . meanwhile , Scheduling information data will also work with robustness detection modules , Select the appropriate target upstream server for each connection . If you use hash Load balancing scheduling method , You can use $remote_addr( client ip) To achieve a simple persistent conversation ( Same client ip The connection of , Always fall into the same service server On ).
And others upstream The modules are the same ,tcp Of stream The module also supports defining the forwarding weight of load balancing ( To configure “weight=2”), also backup and down Parameters of , Used to kick down failed upstream servers .max_conns Parameters can limit the tcp Number of connections , Set the appropriate configuration value according to the capacity of the server , Especially in high concurrency scenarios , It can achieve the purpose of overload protection .
nginx Monitor client connections and upstream connections , Once the data is received , be nginx Will immediately read and push to the upstream connection , Can not do tcp Data detection within the connection .nginx Maintain a memory buffer , Used for writing client and upstream data . If the client or server transmits a large amount of data , The buffer will increase the memory size appropriately .
When nginx Receive a close connection notification from either party , perhaps tcp The connection is idle more than proxy_timeout Configured time , The connection will be closed . about tcp A long connection , We should choose the right proxy_timeout Time for , meanwhile , Focus on Monitoring socke Of so_keepalive Parameters , Prevent premature disconnection .
ps: Service robustness monitoring
tcp Load balancing module supports built-in robustness detection , If an upstream server refuses tcp Connect more than proxy_connect_timeout Configured time , Will be considered invalid . under these circumstances ,nginx Try connecting now upstream Another normal server in the group . The connection failure message will be logged to nginx In the error log of .
If a server , Repeated failure ( More than the max_fails perhaps fail_timeout Configured parameters ),nginx And I'll kick this server . The server was kicked out 60 Seconds later ,nginx Occasionally try to reconnect it , Check if it's back to normal . If the server returns to normal ,nginx Add it back to upstream Within the group , Slowly increase the proportion of connection requests .
Where “ Slowly increase ”, Because usually a service has “ Hot data ”, in other words ,80% More than that , The reality will be blocked in “ Hot data cache ” in , There are only a few requests that actually execute processing . When the machine just started ,“ Hot data cache ” It's not actually established , At this time, a large number of requests are forwarded explosively , It's likely that the machine won't be able to “ To bear ” And hang up again . With mysql As an example , our mysql Inquire about , Usually 95% All the above are in memory cache in , There are not many queries that actually execute .
Actually , Whether it's a single machine or a cluster , In the high concurrent request scenario , Restart or switch , There is a risk , There are two main solutions :
(1) Requests gradually increase , From less to more , Gradually accumulate hot data , Finally reach the normal service state .
(2) Be prepared in advance “ Commonly used ” The data of , Take the initiative to do “ preheating ”, After preheating , Open the access of the server again .
tcp Load balancing principle and lvs Wait is the same , Work at a lower level , The performance will be higher than the original http A lot of load balancing . however , No comparison lvs Better ,lvs Is placed in the kernel module , and nginx Working in user mode , and ,nginx Relatively heavy . Another point , It's a great pity , This module is actually a paid function .
tcp Load balancing module supports built-in robustness detection , If an upstream server refuses tcp Connect more than proxy_connect_timeout Configured time , Will be considered invalid . under these circumstances ,nginx Try connecting now upstream Another normal server in the group . The connection failure message will be logged to nginx In the error log of .
If a server , Repeated failure ( More than the max_fails perhaps fail_timeout Configured parameters ),nginx And I'll kick this server . The server was kicked out 60 Seconds later ,nginx Occasionally try to reconnect it , Check if it's back to normal . If the server returns to normal ,nginx Add it back to upstream Within the group , Slowly increase the proportion of connection requests .
Where “ Slowly increase ”, Because usually a service has “ Hot data ”, in other words ,80% More than that , The reality will be blocked in “ Hot data cache ” in , There are only a few requests that actually execute processing . When the machine just started ,“ Hot data cache ” It's not actually established , At this time, a large number of requests are forwarded explosively , It's likely that the machine won't be able to “ To bear ” And hang up again . With mysql As an example , our mysql Inquire about , Usually 95% All the above are in memory cache in , There are not many queries that actually execute .
Actually , Whether it's a single machine or a cluster , In the high concurrent request scenario , Restart or switch , There is a risk , There are two main solutions :
(1) Requests gradually increase , From less to more , Gradually accumulate hot data , Finally reach the normal service state .
(2) Be prepared in advance “ Commonly used ” The data of , Take the initiative to do “ preheating ”, After preheating , Open the access of the server again .
tcp Load balancing principle and lvs Wait is the same , Work at a lower level , The performance will be higher than the original http A lot of load balancing . however , No comparison lvs Better ,lvs Is placed in the kernel module , and nginx Working in user mode , and ,nginx Relatively heavy . Another point , It's a great pity , This module is actually a paid function .
That's all “ stay Nginx How to configure the server for TCP Load balancing of ” All the content of this article , Thank you for reading ! I believe you will gain a lot after reading this article , Xiaobian will update different knowledge for you every day , If you want to learn more , Please pay attention to the Yisu cloud industry information channel .
copyright notice
author[Billion cloud speed],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291744570868.html
The sidebar is recommended
- How to use HTML to display segmentation
- How to define and use HTML box model
- How to use the box pack attribute and box align attribute inside the box in HTML
- What are the differences and relationships among HTML, CSS and JS
- How to save home page as HTML
- How to solve the post request return 405 of nginx reverse proxy to HTML page
- How to upload pictures without refresh with HTML5 + PHP
- How to define HTML text tags, pictures, paths, hyperlinks and anchors
- How to upload static HTML files to the host or server
- How to use calculated and watch in Vue
guess what you like
How does Vue Preview PDF, word, xls, PPT and txt files
Can jQuery listen for events
Luxury cars "senseless price increase", the configuration of the new Porsche Macan remains unchanged, with a maximum increase of 19000 yuan
1-ch579m program upgrade OTA (self built Internet of things platform) - ch579m uses its own Ethernet to remotely download and upgrade MCU program through HTTP (MCU program rotation check and update)
The front-end downloads files, and the back-end sends gzip files. Is there a way to get the file size?
Why does Vue route jump refresh the page?
The conversion code of Babel in the project needs to include node_ Modules
[nginx] prefix removal in nginx proxy pass configuration
Vue packaging error module build failed: typeerror: this getOptions is not a function
Use of I18N in Vue
Random recommended
- Who is bunk, the new hero of endless dungeon? Introduction to new hero bunk skills
- Front end entry first bullet | development tools and configuration
- Nice HTML login page
- Vue error prone summary
- Summary of material resources website (icon | wallpaper | Adobe plug-in | jQuery plug-in | badge | online conversion | color splicing | continuous update)
- Step by step, configure nodesjs environment with hbuilder for Vue JS development
- Vue sets the global websocket and accepts message notification processing on the corresponding page
- Solve the problem that vuex data is reset during page refresh
- Java data structures and algorithms lesson 2 - generics
- Leetcode-82 - delete duplicate Element II in the sorting linked list
- [front end] quickly create HTML
- Vue router routing navigation
- Vue router redirection and alias
- Vue router named routes, (nested) named views
- Dynamic components & slots & custom instructions for getting started with Vue Foundation
- JavaScript algorithm -- heap sorting
- K8s deployment-43-take you to learn ingress nginx (Part 2)
- Detailed breakdown of actual combat of CMS background system project (VII)
- Explore the store | new architecture blessing, performance and safety. This style is super personality_ Integra_ Honda_ platform
- Sports kit blessing / replacement of four cylinder engine Chevrolet chuangku RS live shooting analysis_ Aspect_ Display screen_ Complete vehicle
- Talking about nodejs server
- Node. js&lt; I & gt—— Encounter node and repl usage
- Vue basic API: calculation attribute + filter + listener
- 1-stm32 + mn316 (nb-iot) remote upgrade OTA (self built Internet of things platform) - STM32 uses HTTP to download program files and upgrade programs through mn316 (MCU program rotation check and update)
- Vue Axios response interception
- vue3 ref
- How does Vue transfer the data from the parent component to the child component intact?
- The back-end interface developed by springboot in idea and the Vue front-end developed by vscode. How to integrate Vue code into springboot?
- Fried cold rice series 4: scope and closure in JavaScript
- Typescript type compatibility learning
- Summary of bugs encountered in front-end development
- Chrome developer tool: performance analysis using web panel
- Collation of common semantic elements and global attributes in HTML
- Life cycle in Vue
- 5.1 fear of traffic jam? With a budget of less than 100000, these cars with adaptive cruise make it easy for you to travel
- Docker compose deploy nginx configure SSL
- The content of element type “mapper“ must match “(cache-ref|cache|resultMap*|parameterMap*|sql*|inse
- -CSS-
- Vue uses two-way binding to implement the user registration page
- Is Infiniti qx60 worth less than 400000 yuan? It depends on the discount