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 Kubernetes pod orchestration and lifecycle

Table of contents K8S Master Basic Architecture P...

The latest 36 high-quality free English fonts shared

01. Infinity Font Download 02. Banda Font Download...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

Detailed explanation of DOM DIFF algorithm in react application

Table of contents Preface What is VirtualDOM? Rea...

React useMemo and useCallback usage scenarios

Table of contents useMemo useCallback useMemo We ...

Our thoughts on the UI engineer career

I have been depressed for a long time, why? Some t...

Teach you how to build a react+antd project from scratch

The previous articles were all my own learning lo...

How to implement interception of URI in nginx location

illustrate: Root and alias in location The root d...

MySQL should never write update statements like this

Table of contents Preface cause Phenomenon why? A...

Example of using CSS to achieve semi-transparent background and opaque text

This article introduces an example of how to use ...

Front-end JavaScript operation principle

Table of contents 1. What is a JavaScript engine?...

Ubuntu installation Matlab2020b detailed tutorial and resources

Table of contents 1. Resource files 2. Installati...