Details of nginx reverse proxy, load balancing, and IP hash implementation

Keywords: Nginx vim curl

1. Implementation of nginx reverse proxy

Three virtual machines are required:

Virtual machine ip function

server1 172.25.1.1 reverse proxy server (build nginx)
server2 172.25.1.2 backend server
server3 172.25.1.3 back end server

(1) Making soft links for nginx

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

(2) Modify profile

[root@server1 nginx]# cd conf/
# Add to 15,16Yes, and55-60That's ok
[root@server1 conf]# vim nginx.conf
//Amendment:
 12 http {
 13     include       mime.types;
 14     default_type  application/octet-stream;
 15         upstream westos {
 16                 server 172.25.63.3:80;			#Back end server ip and port
 17 
 18 }
.......
 55 server {
 56         listen 80;
 57         server_name www.westos.org;
 58         location / {
 59                 proxy_pass http://westos;
 60 }
 61 }

After the modification is completed, use the following command to check for errors:

[root@server1 conf]# nginx -t    #Check for syntax errors
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Open (when nginx is not open) or reload (when nginx is open):

[root@server1 conf]# nginx			#Open nginx
[root@server1 conf]# nginx -s reload		#Reload nginx

(3) Configure backend server

Both server2 and server3 virtual machines install httpd and write it to the test page:

[root@server2 ~]# vim /var/www/html/index.html
[root@server2 ~]# cat /var/www/html/index.html
server2
[root@server2 ~]# systemctl start httpd
[root@server3 ~]# vim /var/www/html/index.html
[root@server3 ~]# cat /var/www/html/index.html
server3
[root@server3 ~]# systemctl start httpd

test

Test on the real machine: (note whether there is domain name resolution at www.westos.org on the real machine)

[root@foundation1 ~]# curl www.westos.org
server3					#Reverse proxy succeeded
[root@foundation1 ~]# curl www.westos.org
server3
[root@foundation1 ~]# cat /etc/hosts
172.25.1.1 www.westos.org

2. Implementation of nginx load balancing

This experiment is basically the same as the previous experiment. The only difference is the configuration file:
On server1:
Edit the VIM nginx.conf file to add line 16

[root@server1 conf]# vim nginx.conf 
 12 http {
 13     include       mime.types;
 14     default_type  application/octet-stream;
 15         upstream westos {
 16                 server 172.25.63.2:80;
 17                 server 172.25.63.3:80;
 18 
 19 }
.......
 56 server {
 57         listen 80;
 58         server_name www.westos.org;
 59         location / {
 60                 proxy_pass http://westos;
 61 }
 62 }

Reload after checking for errors:

[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

Test:
In real machine

[root@foundation1 ~]# curl www.westos.org
server2
[root@foundation1 ~]# curl www.westos.org
server3
[root@foundation1 ~]# curl www.westos.org
server2
[root@foundation1 ~]# curl www.westos.org

nginx also has fault detection for back-end servers

When the server 2 service is down:

[root@server2 ~]# systemctl stop httpd

Retest:

3. One ip address can only access one server

Sometimes an ip address can only access one server instead of switching between two servers. In this case, the configuration file of server1 needs to be changed as follows:
Add line 16 based on the above experiment

[root@server1 conf]# vim nginx.conf
 12 http {
 13     include       mime.types;
 14     default_type  application/octet-stream;
 15         upstream westos {
 16                 ip_hash;
 17                 server 172.25.63.2:80;
 18                 server 172.25.63.3:80;
 19 
 20 }
........
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

Test:
It realizes that a domain name is only dispatched to one server

4. Use server1 as the standby server of server3

(1) Edit nginx profile
On server1:
Edit vim nginx.conf file, log off 16 lines, modify 17 lines

[root@server1 conf]# vim nginx.conf

 12 http {
 13     include       mime.types;
 14     default_type  application/octet-stream;
 15         upstream westos {
 16                 #ip_hash;
 17                 server 127.0.0.1:80 backup;
 18                 server 172.25.63.3:80;
 19 
 20 }

(2) Edit nginx test page on server
On server1:

[root@server1 nginx]# cd html/
[root@server1 html]# echo server1 > index.html
[root@server1 html]# cat index.html 
server1

Reload after:

[root@server1 html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 html]# nginx -s reload

(3) Testing

On the real machine:

Published 99 original articles, won praise 1, visited 1851
Private letter follow

Posted by Drezard on Thu, 05 Mar 2020 23:59:04 -0800