Nginx practical application load balancing

Bobo roast duck 2020-11-13 05:00:09
nginx practical application load balancing

In this article, we continue to introduce nginx Actual operation of , Here's how Nginx Implementation of load balancing based on .

Load balancing

 Insert picture description here
Let's take a look at the above load balancing diagram , You'll find that it's very similar to the reverse agent we talked about last time, isn't it ? If the real server has more than one, is it the structure diagram above us ? So what is load balancing ? In fact, on the basis of reverse proxy, users' requests are forwarded to the corresponding real services through various load strategies .

The general principle of network load balancing is to allocate the network load balance to each operation unit of network cluster by using a certain allocation strategy , It enables a single heavy load task to be shared to multiple units for parallel processing , Make a large number of concurrent access or data traffic shared on a number of units to deal with separately , So as to reduce the waiting response time of users
upstream yes Nginx Of HTTP Upstream modular , This module uses a simple scheduling algorithm to implement the client IP Load balancing to back end servers

Nginx Load balancing practice

1. Create three web service

On the basis of the last article, we create two more services , The ports of the three services are 8081 8082 8083.

 Insert picture description here

2.Nginx To configure

upstream block
upstream name{…}

upstream gupao{

server {

location / {

proxy_pass http://gupao;

server block
grammar :

server address [parameters]
form explain
server Mandatory
address Mandatory It can be the host name 、 domain name 、ip, Port number can also be specified
parameters Optional
down: At present server deactivated
backup: At present server It's a backup server , There is nothing but backup The back-end servers are all hung up or busy before they can be allocated to the requests
weight: At present server Load weight , The greater the weight, the greater the chance of being requested . The default is 1
max_fails and fail_timeout Generally used in association , If a station server stay fail_timeout In time max_fails Secondary connection failed , that Nginx Think it's dead , Thus in fail_timeout No more requests in time ,fail_timeout The default is 10s,max_fails The default is 1, That is, by default, if there is an error, it will be considered that the server is down , If you will max_fails Set to 0, It means the inspection is cancelled .

 Insert picture description here

Upstream Module scheduling algorithm

1. polling ( Default ): Each request is allocated to different back-end servers in chronological order , If the backend server down fall , Can be eliminated automatically
 Insert picture description here

2.Ip_hash: Per request by access ip Of hash Result distribution , In this way, each visitor has fixed access to a back-end server , Can solve session The problem of
 Insert picture description here

3. The weight : That is, you can specify the polling rate ,weight Proportional to the chance of access , It is mainly used in heterogeneous back-end servers
 Insert picture description here

4. response time : Allocate requests fairly according to the server's response time , It means the priority allocation with the shortest response time on the back end
 Insert picture description here
 Insert picture description here
Check the configuration file and the prompt is not recognized fair This is because fair Belongs to a third party module , It's not in the official package . The official download
Then decompress

 Not installed Nginx
Switch to Nginx The directory performs an operation
To configure :
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/ --add-module=/home/nginx-upstream-fair-master
Compilation and installation
make && make intstall
Installed Nginx
Switch to Nginx The directory performs an operation
To configure
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/ --add-module=/home/nginx-upstream-fair-master
Copy Nginx
cp objs/nginx /usr/local/nginx/nginx

 Insert picture description here

5.url_hash: and iphash similar ,urlhash The visit is based on url Of hash Results to allocate requests , Make the same url Access to the same server

 Insert picture description here
You also need to install third-party plug-ins

本文为[Bobo roast duck]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple