Nginx multiple servers to achieve load balancing

Nginx load balancing server: IP: 192.168.0.4 (Nginx-Server)

List of web servers:

Web1: 192.168.0.5(Nginx-Node1/Nginx-Web1)

Web2:192.168.0.7(Nginx-Node2/Nginx-Web2)

Purpose: When users access Nginx-Server, load balancing to Web1 and Web2 servers through Nginx.

nginx.conf for Nginx load balancing server

The configuration annotations are as follows:

events { use epoll; worker_connections 65535; } http { ##Upstream load balancing, four scheduling algorithms## #Scheduling algorithm 1: round robin. Each request is allocated to different backend servers one by one in chronological order, #if the When a server on the end is down, the faulty system is automatically eliminated, so that user access is not affected upstream webhost { server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; } #Scheduling algorithm 2: weight (weight). Can be based on the machine The configuration defines the weight. The higher the weight, the greater the probability of being assigned upstream webhost { server 192.168.0.5:6666 weight=2; server 192.168.0.7:6666 weight=3; } #Scheduling algorithm 3:ip_hash. Each request is accessed by IP hash result allocation, so that visitors from the same IP can access a back-end server, #effectively solve the session sharing problem existing in dynamic web pages upstream webhost { ip_hash; server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; } #Scheduling algorithm 4: url_hash (a third-party plug-in needs to be installed). This method allocates requests according to the hash result of accessing the url. #Directing each url to the same back-end server can further improve the efficiency of the back-end cache server. #Nginx itself does not support url_hash. If you need to use this scheduling algorithm, you must install the Nginx hash package upstream webhost { server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; hash $request_uri; } #Scheduling algorithm 5 :fair (a third-party plug-in needs to be installed). This is a more intelligent load balancing algorithm than the above two. #This algorithm can intelligently perform load balancing according to the page size and loading time, that is, according to the response time of the backend server To allocate requests, #Priority allocation with short response time.Nginx itself does not support fair. If you need to use this scheduling algorithm, you must download the upstream_fair module of Nginx

#Configuration of virtual host (using scheduling algorithm 3: ip_hash) server { listen 80; server_name mongo.demo.com; #Enable reverse proxy for "/" location / { proxy_pass http://webhost ; proxy_redirect off; proxy_set_header X- Real-IP remote_addr; #The back-end web server can obtain the user's real IP through X-Forwarded-For proxy_set_header X-Forwarded-Forproxy_add_x_forwarded_for; #The following are some reverse proxy configurations, optional. proxy_set_header Host $host; client_max_body_size 10m; #Maximum number of bytes of a single file that clients request client_body_buffer_size 128k; #Buffer proxy buffers client requests The maximum number of bytes, proxy_connect_timeout 90; #nginx connects to the backend server connection timeout (proxy connection timeout) proxy_send_timeout 90; #backend server data return time (proxy send timeout) proxy_read_timeout 90; #After the connection is successful, the backend server responds Time (proxy reception timeout) proxy_buffer_size 4k; #Set the buffer size of the proxy server (nginx) to save the user header information proxy_buffers 4 32k; #proxy_buffers buffer, the average web page is set below 32k proxy_busy_buffers_size 64k; #Buffer size under high load ( proxy_buffers*2) proxy_temp_file_write_size 64k; #Set the size of the cache folder, if it is larger than this value, it will be transmitted from the upstream server} } } Configure 192.168.0.4 (Nginx-Server)

Create a folder to store configuration files

mkdir -p /opt/confs vim /opt/confs/nginx.conf events { use epoll; worker_connections 65535; } http { upstream webhost { ip_hash; server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; } server { listen 80; server_name mongo.demo.com; location / { proxy_pass http://webhost; proxy_redirect off; proxy_set_header X-Real-IP remote_addr;  proxy_set_header X-Forwarded-Forproxy_add_x_forwarded_for; proxy_set_header Host $host; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } } 启动负载均衡服务器192.168.0.4(Nginx-Server)

Configure 192.168.0.5 (Nginx-Node1/Nginx-Web1)

Create a folder for storing web pages

mkdir -p /opt/htmlvim /opt/html/index.html edit the content as follows:

The host is 192.168.0.5 - Node 1

Start 192.168.0.5 (Nginx-Node1/Nginx-Web1)

Configure 192.168.0.7 (Nginx-Node2/Nginx-Web2)

Create a folder for storing web pages

mkdir -p /opt/htmlvim /opt/html/index.html edit the content as follows:

The host is 192.168.0.7 - Node 2

Start 192.168.0.7 (Nginx-Node2/Nginx-Web2)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324142608&siteId=291194637