The agent

To understand reverse proxies, you first need to know what a forward proxy is. To understand forward proxies, you need only know what a proxy is. Agent is an intermediary in fact, in different things or the same thing to play a link between the link. Such as ticket scalpers, real estate agents and so on.

Proxy in the Internet refers more to a proxy server, which is located between a client and a server and acts as an intermediary between the two. This kind of agent is more common in life, such as what we often say to build a ladder to access the Internet, using is the agent technology.

Forward agent

“Positive agent (forward proxy)” : is located in a client and server (proxy server), between the target server to get content from the target server, the client sends a request to the proxy server and specify the target, then the proxy server to the target server transfer request and will get back to the content of the client.

For example, to visit Google in China, direct access is not possible. We can use a forward proxy server that can access Google to send requests to the proxy server, and the proxy will go to Google to obtain data and return it. In this way, the demand for accessing Google can be realized in a disguised way.

In a word, “summary” : forward proxy, is the proxy server proxy client, to interact with the target server.


“Use of forward proxy” :

  • The IP address access restriction is exceeded
  • Caching speeds up access to resources
  • Hide the real IP address of the client
  • Client access authorization

The reverse proxy

Reverse Proxy: Reverse Proxy is the Reverse Proxy server that represents the server. The proxy server receives the request from the client, forwards the request to the server on the Intranet, and returns the result to the client. In this case, the proxy server acts as a reverse proxy server.

The reverse proxy server is located between the user and the target server. However, for users, the reverse proxy server is the target server. Users can access the reverse proxy server directly to obtain the resources of the target server. At the same time, users do not need to know the address of the target server, nor do they need to make any Settings on the client side.

A reverse proxy is a proxy server that proxies the target server to interact with the client.


“Purpose of reverse proxy” :

  • Hide the real SERVER IP address
  • Load balancing
  • Caching speeds up access to resources
  • Provide security

Load balancing

In the actual production environment, the reverse proxy server may have more than one target server. For example, a developed application is deployed on a Tomcat server, and the maximum concurrency limit of Tomcat is only about 200 by default if it is not optimized. In this case, in order to solve the problem of high concurrency, we can only choose to replace servers or build multiple servers to solve the concurrency problem through reverse proxy and load balancing technology.


A Load Balance is a server cluster composed of multiple servers in a symmetric manner. Each server has equal status and can provide services independently without the assistance of other servers. Through some load management technique, “central requests” from outside are evenly distributed to “one server” in a symmetrical structure.

conclusion

Forward proxy, where the proxy server proxies the client to interact with the target server. Real-life scenario: scalpers buy tickets.

Reverse proxy, where the proxy server proxies the target server to interact with the client. A real-life scenario: a real estate agent.

Nginx reverse proxy and load balancing


We mentioned earlier that setting up multiple servers and using reverse proxy and load balancing technology can solve the concurrency problem, so where does the realization of load balancing come from? To avoid reinventing the wheel, we chose mature tools that were already available to help us do the job. Nginx is a good choice.


Nginx was developed by Igor Sysoev for https://www.rambler.ru/, The second most visited site in Russia. Nginx is a high-performance HTTP and reverse proxy server that can withstand around 5W concurrent requests. Ngnix can serve as a reverse proxy server on the one hand and a static resource server on the other hand. This article focuses on how to use Nginx reverse proxy and load balancing.

Website: http://nginx.org/

The environment

  • threeCentOS 7.8.2003The machine
  • 192.168.10.101The installationNginx 1.18.0
  • 192.168.10.102192.168.10.103The installationTomcat 9.0.38

Install Nginx

Download resources

Download Nginx and unzip it.

#Download the nginx zip package
Wget -p/usr/local/SRC, http://nginx.org/download/nginx-1.18.0.tar.gz#Unpack the
Tar -zxvf /usr/local/src/nginx-1.18.0.tar.gz -c /usr/local/srcCopy the code

Install dependencies

Nginx is based on C language development, HTTP module uses PCRE to parse regular expressions, and can use Zlib to gzip the content of THE HTTP package, Nginx not only supports HTTP protocol, but also supports HTTPS, HTTPS requires OpenSSL support, so you must install the environment on which Nginx depends before installing it.

yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl-devel
Copy the code

Install Nginx

Compile and install.

#Switch to the unzip directory of nginx
CD/usr/local/SRC/nginx - 1.18.0#Create the nginx installation directory
mkdir -p /usr/local/nginx
#Specify the installation path for nginx
./configure --prefix=/usr/local/nginx/ #Compile and install make && make install Copy the code

Common commands

#Switch directory
cd /usr/local/nginx

#Start the
sbin/nginx
 #restart sbin/nginx -s reload  #stop sbin/nginx -s quit Copy the code

access

After startup Nginx, browser visit: http://192.168.10.101:80/ the results are as follows:


Install Tomcat

Download resources

Download Tomcat and unzip it.

#Download the Tomcat package
wget -P /usr/local/src https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.38/bin/apache-tomcat-9.0.38.tar.gz
#Create a directory
mkdir -p /usr/local/tomcat
#Unpack the
Tar -zxvf /usr/local/src/apache-tomcat-9.0.38.tar.gz -c /usr/local/tomcatCopy the code

Modify the page

Modify the index.jsp page in Tomcat ROOT project.

#Editing the JSP
Vim/usr/local/tomcat/apache tomcat - 9.0.38 / webapps/ROOT/index. The JSPCopy the code

Add arbitrary dot content to the body tag to distinguish between servers.

<h1 style="color:red">192.169.10.102:8080</h1>
<h1 style="color:red">192.169.10.103:8080</h1>
Copy the code


Start the visit

Run the /usr/local/tomcat/apache-tomcat-9.0.38/bin/startup.sh command to start tomcat.

Start Tomcat, browser visit: http://192.168.10.102:8080/ and http://192.168.10.103:8080/ results are as follows:



Configure Nginx

Vim/usr/local/nginx/conf/nginx. Conf edit the configuration file.

Under the HTTP node, add the upstream node. Define a set of servers using upstream name{} syntax.

Then add the following figure under port 80 of the server node. By default, Nginx distributes requests to each server in a weighted polling manner. When weight is not specified, all servers have the same weight. Configure the Nginx startup user based on the actual environment.


Reverse proxy load balancing

Through the above process, we have completed the requirements of implementing reverse proxy Tomcat server cluster and load balancing based on Nginx. After Nginx is restarted, access to Nginx is routed to the proxyed Tomcat server using a polling (default) load balancing algorithm. Central client requests to Nginx are evenly distributed to each Tomcat machine at a time. More details on Nginx configuration and load balancing algorithms will be covered in another article later.


This article is licensed under a Creative Commons attribution – Noncommercial – No Deductive 4.0 International license.

You can check out more Nginx articles in the category below.

🤗 your likes and retweets are the biggest support for me.

📢 follow the public account Mr. Hello Ward “document + video” each article is equipped with a special video explanation, learning more easily oh ~