How to configure Nginx load balancing

How to configure Nginx load balancing

Nginx load balancing configuration

It is also relatively simple to configure load balancing using nginx

First, configure the address corresponding to the virtual domain name in the http block

# Load balancing upstream myserver {
 server 127.0.0.1:8080;
 server 127.0.0.1:8082;
}

Then configure the listener in the server block

server {
 listen 9000;
 server_name localhost;

 location / {
  root html;
  index index.html index.htm;
  # Corresponding to the name proxy_pass http://myserver configured in the upstream above;
  
  ### The following are minor items of concern proxy_set_header Host $host;
  proxy_method POST;

   #Specify the header field that is not forwarded proxy_hide_header Cache-Control;

   #Specify the forwarding header field proxy_pass_header Server-IP;

   # Whether to forward the package body proxy_pass_request_body on | off;

   # Whether to forward headers proxy_pass_request_headers on | off;

   # Visible/invisible URI, when upstream redirection occurs, whether Nginx changes the URI synchronously
  proxy_redirect on | off;
 }
}

In this way, when accessing the server using port 9000, load calls will be made to port 8080 and 8082.

Nginx load balancing strategy

Polling (default)

Distribute them to different servers one by one in chronological order. If the backend server crashes, they will be automatically deleted.

Weight

Weight represents the weight, the default value is 1, the larger the weight, the more requests are allocated

# Load balancing upstream myserver {
 server 127.0.0.1:8080 weight=1;
 server 127.0.0.1:8082 weight=2;
}

ip_hash

Each request is hashed according to the accessed IP address, so that each visitor accesses a certain server, which can solve the session problem.

# Load balancing upstream myserver {
  ip_hash;
 server 127.0.0.1:8080;
 server 127.0.0.1:8082;
}

fair (third party)

Assign according to the response time of the request, which server responds faster is assigned to whom

The above is the details of how to configure load balancing in Nginx. For more information about configuring load balancing in Nginx, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • 4 configuration examples of Nginx load balancing
  • Nginx load balancing configuration, automatic switching mode when downtime occurs
  • Nginx load balancing configuration simple configuration method
  • 5 ways to configure nginx load balancing
  • Analysis of Nginx cluster load balancing configuration process
  • Nginx simple load balancing configuration example
  • Detailed explanation of Linux system configuration nginx load balancing
  • Simple configuration method of nginx load balancing
  • Super detailed nginx load balancing configuration

<<:  Pure CSS to achieve the water drop animation button in Material Design

>>:  Vue realizes the function of book shopping cart

Recommend

Detailed explanation of the processing of the three Docker Nginx Logs

Because colleagues in the company need Nginx log ...

A brief analysis of CSS :is() and :where() coming to browsers soon

Preview versions of Safari (Technology Preview 10...

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

MySQL series multi-table join query 92 and 99 syntax examples detailed tutorial

Table of contents 1. Cartesian product phenomenon...

Detailed explanation of the loop form item example in Vue

Sometimes we may encounter such a requirement, th...

Use of vuex namespace

Table of contents Since Vuex uses a single state ...

Master the commonly used HTML tags for quoting content in web pages

Use blockquote for long citations, q for short ci...

How to install Docker on Windows Server 2016

Recently Microsoft released Windows Server 2016, ...

Solution to nginx-ingress-controller log persistence solution

Recently I saw an article on a public account tha...

VMware ESXi installation and use record (with download)

Table of contents 1. Install ESXi 2. Set up ESXi ...

Detailed explanation of LVM seamless disk horizontal expansion based on Linux

environment name property CPU x5650 Memory 4G dis...

How to modify create-react-app's configuration without using eject

1. Why is eject not recommended? 1. What changes ...