nginx is an example of load balancing as a reverse proxy

  • 2020-05-06 12:13:36
  • OfStack

The lightweight, high-performance web server does two main things:

> directly as http server(instead of apache, PHP requires FastCGI processor support);
Another > feature is load balancing
as a reverse proxy server
Here's an example of how to use nginx for load balancing. Because of nginx's advantages in dealing with concurrency, this application is now very common. Of course, Apache's mod_proxy and mod_cache can also be used in combination to achieve reverse proxy and load balancing for multiple app server, but apache is still not as good at concurrent processing as nginx.

1) environment:

a. We have a local Windows system and then use VirutalBox to install a virtual Linux system.
Install nginx(listens to port 8080) and apache(listens to port 80) on your local Windows system, respectively. Install apache(listens to port 80) on a virtual Linux system.
In this way, we have one nginx in the front as a reverse proxy server; There are two apache behind as application servers (think of them as small server cluster). ; -));

b. nginx is used as a reverse proxy server, placed before the two apache, as a user access point;
nginx only handles static pages. Dynamic pages (php requests) are all delivered to two apache in the background.
In other words, we can put the static pages or files of our website in the nginx directory; Dynamic page and database access is reserved to the apache server in the background.

c. The following two methods are introduced to achieve load balancing of server cluster.
We assume that the front-end nginx(127.0.0.1:80) contains only a static page index.html;
Two apache servers in the background (localhost:80 and 158.37.70.143:80, respectively), a root directory to put the phpMyAdmin folder and test.php (the test code is print "server1";) , the other root directory just puts an test.php (the test code is print "server2";) .

2) load balancing for different requests:

a. In the simplest way to build a reverse proxy (nginx only handles static rather than dynamic content, which is handled by apache server in the background), we specifically set
to: modify
in PHP.conf
 
location ~ \.php$ { 
proxy_pass 158.37.70.143:80 ; 
} 

> so when the client access localhost: 8080 / index html, front-end nginx will automatically response;
> when the user visits localhost: 8080 / test php when nginx directory (this time there is no this file), but through the Settings above location ~ \. php $(say regular expression match. php at the end of the file, the details see location is how to define and matching http: / / wiki nginx. org/NginxHttpCoreModule), nginx server will automatically pass to 158.37.70.143 apache server. Es123en.php under this server will be automatically parsed, then the html result page will be returned to nginx, nginx will be displayed (nginx can also support caching if nginx USES memcached module or squid), and the output result will be printed server2.

This is the simplest example of using nginx as a reverse proxy server.

b. We now extend the above example to support the above two servers.
We set the nginx.conf module part of server and change the corresponding part to
 
location ^~ /phpMyAdmin/ { 
proxy_pass 127.0.0.1:80 ; 
} 
location ~ \.php$ { 
proxy_pass 158.37.70.143:80 ; 
} 

First part above location ^ ~ / phpMyAdmin, said not to use regular expression match (^ ~), but direct match, namely if the client visit URL is http: / / localhost: 8080 / phpMyAdmin/beginning no phpMyAdmin nginx directory directory (local), nginx automatically pass to 127.0.0.1:80 Apache server, The server parses the pages in the phpMyAdmin directory and sends the results to nginx, which displays the results.
If the client access URL is http: / / localhost test php, would be pass 158.37.70.143:80 apache for processing.

So in summary, we implemented load balancing for different requests.
If the user visits the static page index.html, the front-end nginx directly responds.
If the user visits test.php, Apache of 158.37.70.143:80 responds.
If the user accesses the page under the directory phpMyAdmin, Apache of 127.0.0.1:80 responds.

3) load balancing for accessing the same page:
namely user access http: / / localhost: 8080 / test php the same page, we can achieve the load balancing two servers (in practice, the two synchronous data on the server request, here we define the print server1 and server2 was conducted to identify the difference between).

a. Now we have a situation where nginx is localhost listening on port 8080 under windows;
Two apache, one is 127.0.0.1:80(including test.php page but printing server1) and the other is 158.37.70.143:80 of the virtual machine (including test.php page but printing server2).

b. Therefore reconfigure nginx.conf is:
> is first added in nginx's configuration file nginx.conf's http module, the definition of server cluster server cluster(we have two here) :
 
upstream myCluster { 
server 127.0.0.1:80 ; 
server 158.37.70.143:80 ; 
} 

Means that this server cluster contains two servers
> is then defined in the server module, load balancing:
 
location ~ \.php$ { 
proxy_pass http://myCluster ; # The name here and the name above cluster Same name  
proxy_redirect off; 
proxy_set_header Host $host; 
proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
} 

So if access http: / / localhost: 8080 / test php page, nginx don't have the file directory, but it will automatically defines its pass to myCluster service fleet, respectively by 127.0.0.1:80; Or 158.37.70.143:80; So let's do that.
When upstream is defined above, no weight is defined after each server, indicating the equilibrium between them. If you want a more specific response, for example:
 
upstream myCluster { 
server 127.0.0.1:80 weight=5; 
server 158.37.70.143:80 ; 
} 

This represents a 5/6 chance of accessing the first server and 1/6 chance of accessing the second. You can also define parameters such as max_fails and fail_timeout.
http://wiki.nginx.org/NginxHttpUpstreamModule

To sum up, we used nginx's reverse proxy server, reverse proxy server, to place it in front of multiple apache server servers.
nginx is only used to handle the static page response and dynamic request of the agent pass, the background apache server as app server to handle the dynamic page from the foreground pass and return to nginx.

Through the above architecture, we can realize the load balancing of cluster and apache.
Two equilibria:
1) access to different contents can be defined in nginx, and agent to different background server; The access phpMyAdmin directory in the example above is proxy to the first server; Visit test.php proxy to the second server;
2) access to the same page can be defined in nginx, balanced (of course, if the server performance can be defined to balance the weight) proxy to different background server. The example above accesses the test.php page and is evenly represented on server1 or server2.
In practice, the same app program and data are reserved on server1 and server2 respectively, and the data synchronization between them needs to be considered.

Related articles: