Using Nginx Server to Realize Reverse Proxy, Load Balancing and Virtual Host

Keywords: Operation & Maintenance Nginx Tomcat Apache IIS

I believe many people have heard of nginx, a small thing slowly swallowing apache and IIS share. So what exactly does it do? Maybe many people don't necessarily understand.

When it comes to reverse proxy, many people may have heard about it, but it is not clear what reverse proxy is. Excerpt from a description on Baidu Encyclopedia:

Reverse Proxy means that the proxy server receives the connection request on the internet, then forwards the request to the server on the internal network, and returns the result from the server to the client requesting the connection on the internet. At this time, the proxy server acts as a server outside.  

It's very straightforward here. In fact, the reverse proxy is a proxy server responsible for forwarding. It seems to act as a real server, but in fact it is not. The proxy server only acts as a forwarding function, and gets the returned data from the real server. In this way, in fact, nginx accomplishes this kind of work. We let nginx listen on a port, such as port 80, but in fact we forward it to tomcat on port 8080 to process the real request. When the request is completed, tomcat returns, but the data is not returned directly at this time, but directly to nginx, which is returned by nginx. Here, we think that nginx is processed, but in fact tomcat is processed.

Speaking of the above approach, many people may recall that static files can be handed over to nginx for processing. Yes, many of the places where nginx is used are static servers, which make it easy to cache static files, such as CSS, JS, html, htm and so on.

Okay, let's start looking at the reverse proxy of the Nginx server, load balancing, and how the virtual host is configured and used.
 

Configuration of Virtual Host


It's launching multiple websites on a single server (it looks like there are multiple hosts).
How to distinguish different websites:
1. Different domain names
Prerequisite: On Windows, configure hosts file 192.168.25.148 www.taobao.com
                                                                       192.168.25.148 www.baidu.com
The purpose is to resolve the corresponding domain name directly into the ip address of our Nginx server without using dns server parsing.
    

server {
        listen       80;			//When accessing the server, the domain name is www.taobao.com and html-taotao
        server_name  www.taobao.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			//The configuration here means accessing the index.html file under html-taobao with the root directory
            root   html-taobao;
            index  index.html index.htm;
			
			//The configuration here indicates that access to the server implements the function of reverse proxy (these two configurations generally do not exist at the same time).
			proxy_pass http://localhost:18081; 
        }
    }
    server {						//When accessing the server, the domain name is www.baidu.com and html-baidu is used.
        listen       80;
        server_name  www.baidu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-baidu;
            index  index.html index.htm;
        }
    }


2. Different ports
The following information must be configured in the hosts file: 192.168.25.148 http://localhost
      

server {			//When accessing port 80, the Nginx server forwards the request to html
			listen       80;
			server_name  localhost;

			#charset koi8-r;

			#access_log  logs/host.access.log  main;

			location / {
				root   html;
				index  index.html index.htm;
			}
		}
		server {			//When accessing port 81, the Nginx server forwards the request to html-81
			listen       81;
			server_name  localhost;

			#charset koi8-r;

			#access_log  logs/host.access.log  main;

			location / {
				root   html-81;
				index  index.html index.htm;
			}
		}

2. Using Nginx to Realize Reverse Proxy


Add mapping relationship between domain name and ip in hosts file
    192.168.25.148 www.sina.com.cn
    192.168.25.148 www.sohu.com    

Two (more) domain names point to the same nginx server, and users visit different domain names to display different web content.
Two (more) domain names are www.sian.com.cn and www.sohu.com, which correspond to a Tomcat server (because a computer runs two servers, so the ports must be different)
    

upstream tomcat1 {
	server 192.168.25.148:8080;
    }
    server {
        listen       80;
        server_name  www.sina.com.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://Tomcat 1; // If the request received is www.sina.com.cn, the request is forwarded to the server http://192.168.25.148:8080.
            index  index.html index.htm;
        }
    }
	
	
    upstream tomcat2 {
	server 192.168.25.148:8081;
    }
    server {
        listen       80;
        server_name  www.sohu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat2; // If the request received is www.sohu.com, the request is forwarded to http://192.168.25.148:8081.
            index  index.html index.htm;
        }
    }


In this way, different requests can be forwarded to the corresponding server.
    

3. Using Nginx to Realize Load Balancing


If a service is provided by multiple servers, it needs to distribute the load to different servers and load balancing.

    upstream tomcat2 {
        server 192.168.25.148:8081;
        server 192.168.25.148:8082;
    }


The server weight can be adjusted according to the actual situation of the server. The higher the weight, the more requests allocated, the lower the weight and the fewer requests. The default is 1

    upstream tomcat2 {
        server 192.168.25.148:8081;
        server 192.168.25.148:8082 weight=2;
    }

IV. High Availability of Nginx


Nginx serves as a load balancer, and all requests go to nginx, so nginx is in a very important position.
If the nginx server goes down, the back-end web service will not be able to provide services, which will have a serious impact.
In order to shield the downtime of load balancing server, it is necessary to build a backup machine.
High Availability monitoring programs are running on both the primary server and the backup machine.
Monitor each other's performance by transmitting information such as "I am alive".
When the backup machine cannot receive such information within a certain period of time, it takes over the service IP of the primary server and continues to provide load balancing services.
When the backup manager receives information like "I am alive" from the main manager, it releases the service IP address.
Such a primary server starts to provide load balancing services again.

Posted by johne281 on Tue, 14 May 2019 22:33:25 -0700