This is the 17th day of my participation in the August More text Challenge. For details, see:August is more challenging

This is the third part of the Nginx series. The first two parts have introduced the Nginx infrastructure and basic configuration. This part will bring you the actual Nginx implementation, and explain how Nginx implements load balancing.

More articles on my Github and personal public number [Whole cliff Road], welcome to watch [front knowledge points], if there is benefit, don’t pay, little hands point a Star.

Read this article

  • Nginx implements load balancing

Upstream is configured for load balancing

  • One of the most attractive aspects of Nginx is that it provides load balancing and reduces server stress
  • Proxy multiple servers, reverse proxy through the upstream module, and keep the system available

Nginx configuration upstream

  • inNginx httpAdd under moduleupstreamnode
"Upstream {# WWW {# WWW} server 10.0.6.108:8090; Server 10.0.0.85:8091; }Copy the code
  • configurationserverUnder the nodelocationNodes in theproxy_passThe reverse proxy
location / { 
    proxy_pass http://www; 
}
Copy the code
  • upstreamLoad is carried out in a polling (default) manner, with each request allocated to a different back-end server one by one in chronological order
  • If back-end serverdownDrop, can take the initiative to eliminate

Upstream’s allocation policy

  • Specify the polling probability (weight),weightProportional to the access ratio, used when the backend server performance is uneven
Upstream linuxIDC {server 10.0.0.77 weight=5; Server 10.0.0.88 weight = 10; } # 10.0.0.88 has twice the access rate of 10.0.0.77Copy the code
  • Each request is assigned based on the hash result of the access IP (IP_hash). In this way, each visitor fixed access to a back-end server, can solvesessionThe problem of
upstream favresin{ 
      ip_hash; 
      server 10.0.0.10:8080; 
      server 10.0.0.11:8080; 
}
Copy the code
  • Requests (fair) are allocated based on the response time of the back-end server. Priority is assigned to those with short response times. Similar to weight allocation strategy.
Upstream favresin {server 10.0.0.10:8080; Server 10.0.0.11:8080; fair; }Copy the code
  • Requests are allocated based on the hash result of the url accesses, so that each URL is directed to the same back-end server (URL_hash). This is effective when the back-end server is cached

  • Note: add hash statement in upstream. No other parameters, such as weight, can be written to the server statement. Hash_method is the hash algorithm used.

Upstream resinserver {server 10.0.0.10:7777; Server 10.0.0.11:8888; hash $request_uri; hash_method crc32; }Copy the code

Upstream setting status value

  • Down: indicates the previous oneserverDo not participate in load temporarily.
  • Weight: defaults to 1,weightThe larger the load, the greater the weight.
  • Max_fails: The number of failed consent requests is 1 by default. When the maximum number is exceeded, returnproxy_next_upstreamError in module definition.
  • fail_timeout: max_failsAfter the second failure. Pause time.
  • Backup: all the others are notbackupThe machinedownOr when you’re busy, askbackupThe machine. So this machine will have the least pressure.
Upstream bakend{# define ip_hash for the Ip address and device status of the load balancer; Server 10.0.0.11:9090 down; Server 10.0.0.11:8080 weight = 2; Server 10.0.0.11:6060; 7070 backup server 10.0.0.11:; }Copy the code

Write in the last

If you find this article helpful, please like it and share it with more people who need it!

Welcome to pay attention to [Quanzhendaolu] and wechat public account [Quanzhendaolu], to get more good articles and free books!
There is a need [Baidu] & [bytedance] & [JD] & [ape counselling] within the push, please leave a message oh, you will enjoy the VIP level extreme speed within the push service ~

Past oliver

Wechat JS API payment implementation

Create a personalized Github profile

The interviewer asks you<img>What elements do you say

Special JS floating point number storage and calculation

Long [word] baidu and good interview after containing the answer | the nuggets technology essay in the future

Front end practical regular expression & tips, all throw you 🏆 nuggets all technical essay | double festival special articles

HTML Tabindex

A few lines of code to teach you to solve wechat poster and TWO-DIMENSIONAL code generation

Vue3.0 Responsive data principle: ES6 Proxy

Read on to make your interviewer fall in love with you

How to draw a fine line gracefully

Front-end performance optimization -HTML, CSS, JS parts

Front-end performance optimization – Page loading speed optimization

Front-end performance optimization – Network transport layer optimization