Nginx configuration load balancing (3 hosts 1 proxy)

Title

A (server)

IP-based virtual host

http://a-ip1 RS1

http://a-ip2 RS2

http://a-ip3 RS3

B (server)

  1. load balancing

  1. The request to access machine B is forwarded to a-ip1 a-ip2 a-ip3, and the received requests are 25% 25% 50%

  1. If there are rs that cannot be provided for 5 consecutive times, the forwarding request will be suspended for 10s

  1. A to record the real client cip

Basic ideas (can be used as a mind map!?)

  1. Due to insufficient hardware conditions, multiple virtual machines can be created

[Implemented by temporarily creating multiple IPs];[It is best to mark it well in the web page to view the experimental results]

[Configure three servers in the main server nginx]

  1. Add the proxy module in the proxy server configuration file

  1. Add max_fail, weight and other configurations after the ip address in the proxy module

  1. Set the format of the log in the main server

: Be sure to use [nginx -t] to check whether the configuration file is wrong after modifying the configuration file!

Configuration steps

##On the main server

  1. Create temporary ip

ip addr add dev ens33 10.3.148.251/24
ip addr add dev ens33 10.3.148.252/24
ip addr add dev ens33 10.3.148.253/24
  • The two parameters behind the countdown can be customized —-[ens33] and [10.3.148.253/24]

  • At present, it is assumed that the temporary ip ends with 251, 252, and 253 respectively

  1. Restart service

systemctl restart network
  1. Ping to see if it doesn’t work

ping xxx.xxx.xxx.xxx
  1. Modify the configuration file (3 main server)

vim /etc/nginx/nginx.conf ##compile installation path
##The following content must be written in the http block##

## The first ip address
server {
           listen 10.3.148.147:80; ##Listen to this ip: port number -80
           server_name _; ##Set the domain name; distinguish different paths under the same ip
           location / {
              root /usr/share/nginx/html; ##doc_root path
              index index.html; ##The html file under the target path under the doc_root path
           }

## The second ip address
server {
             listen 10.3.148.148:80;
             server_name_;
             location / {
                root /usr/share/nginx/html1;
                index index.html;
             }

## The third ip address
server {
           listen 10.3.148.149:80;
           server_name_;
           location / {
               root /usr/share/nginx/html2;
               index index.html;
           }
######done#####

Points to pay attention to when configuring the host file

  • The sever block must be inside the http block

  • Note that the use of {} exists in pairs

  • Port 80 usually needs to be written (default values)

  • This lab uses different ports to represent different hosts

  • You can also create temporary multiple IPs to represent different hosts

  1. Configure the experimental environment (add directory and website)

mkdir /usr/share/nginx/html{1,2}
echo rs2 > /usr/share/nginx/html1/index.html
echo rs3 > /usr/share/nginx/html2/index.html

This purpose is that our experiment is to achieve load balancing

So there must be three different hosts; the environment is limited and there can only be three virtual ips in one virtual machine

Each virtual ip must have a different doc_root

But the directory of nginx we installed is the same

So we need to create multiple html{1, 2, 3} under /usr/share/nginx/ to distinguish doc_root

  1. detection configuration (the most important )

nginx -t
  1. Restart nginx

systemctl restart nginx
  1. Test whether realserver can communicate

Method 1

curl 192.168.40.252
curl 192.168.40.251
curl 192.168.40.250

Method 2

Type this URL directly into your browser

If it is input by domain name

This is best to enter our DNS in the local resolution of the window host before entering (otherwise it will load very slowly)

  1. After all the above operations are completed, you can go to Set up a proxy server! Congratulations on the next step!

##On the proxy server

  1. Edit proxy configuration file[Purpose: Make the server a proxy server]

Because there are three different ips, the module [upstream] is used to contain the three ips

Add an alias after upstream!

It must be the same as the proxy_pass in the server

[Song Ge’s original words ### Configure the proxy to jump to a certain host group at this time]

The configuration file in the proxy server

http{

    upstream ligoudan {
        server 10.3.148.251:80; ##must be in http
        server 10.3.148.251:81; ##outside the server
        server 10.3.148.251:82; ##The purpose is to let the proxy server recognize the 3 ip of the main service
    }

    server {
        listen 80;
        server_name www.gz2301.com;

        location / {
           proxy_pass http://ligoudan; ##The server becomes the key statement of proxy


           # Configure request header related. In order for the terminal rs to receive c-ip
           proxy_set_header Host http_host;
           proxy_set_header X-Real-IP remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           

           # Limit various connection establishment times between proxy-rs
           proxy_connect_timeout 30;
           proxy_send_timeout 60;
           proxy_read_timeout 60;
           

           # speed up rs-->proxy,proxy--client
           proxy_buffering on;
           proxy_buffer_size 32k;
           proxy_buffers 4 128k;
           proxy_busy_buffers_size 256k;
           proxy_max_temp_file_size 256k;
        }
    }
}
  1. Check if the statement is wrong& & amp;Restart the computer

nginx -t
systemctl restart nginx

The above operations load balancing and reverse proxy are completed. Congratulations!

There is a problem (query who has visited the RS log)

Because the reverse proxy is completed, users who access our 3 RS will first go through the PROXY server

Unified proxy server forwards to 3 RS servers

Assume that the server logs of RS are all accessed by the ip address of PROXY

It is impossible to distinguish which RS host is accessed by which user

Therefore, we need to use the modification log format to distinguish the ip address

Mainly use the parameters of http_x_forwarded_for to distinguish (forward)

Modify the log format configuration (usually both the host and the proxy server)

vim /etc/nginx/nginx.conf ### [Configure globally]

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

The following is a detailed explanation of the log format

remote_addr 10.3.135.4 client ip
$remote_user - username
[$time_local] [21/Mar/2023:14:18:58 + 0800] date
request GET / HTTP/1.1 request method URI protocol/version
status 304 http status code
body_bytes_sent 0 request structure send size
http_referer - where the request is redirected from
http_user_agent Mozillaxxx client information
http_x_forwarded_for - where the request was forwarded from

Personal understanding of forwarding and jumping

Jump to:

Because it is a jump, it is understood as cross-domain

Example — For example, we visit Baidu to search for a content “Haolilai official website”, and after the search, we click to enter the official website of Haolilai

This completes the jump [jump from Baidu to Hollyland official website]

Forward:

Forwarding my current only application scenario is proxy reverse proxy

A user accesses 192.168.40.1, this ip address is a proxy server, and several rs hosts are connected behind it

They exist a LAN class

The proxy is equivalent to the commander, instructing the user to access a certain rs host, so that the request is forwarded to the rs host