Nginx as an example of load balancing by reverse proxy _nginx

Source: Internet
Author: User
Tags nginx server phpmyadmin nginx reverse proxy
Nginx This lightweight, High-performance Web server can mainly do two things:

Directly as HTTP server (instead of Apache, PHP requires fastcgi processor support);
Another function is to implement load balancing as a reverse proxy server

Here's an example of how you can use Nginx to achieve load balancing. Because of Nginx's advantages in dealing with concurrency, this application is now very common. Of course, Apache Mod_proxy and Mod_cache can also be used to achieve reverse proxy and load balancing for multiple app servers, but Apache has no nginx expertise in concurrent processing.

1) Environment:

A. We are a local Windows system and then use Virutalbox to install a virtual Linux system.
Install Nginx (listening on port 8080) and Apache (listening on port 80) on the local Windows system separately. Install Apache on a virtual Linux system (listening on port 80).
This is equivalent to having 1 nginx on the front end as a reverse proxy server, followed by 2 Apache as the application server (which can be considered a small server cluster.) ;-) ) ;

B. Nginx used as a reverse proxy server, placed before two Apache, as a user access to the portal;
Nginx only handle static pages, dynamic pages (PHP requests) are all delivered to the background of the two Apache to deal with.
That is, you can put the static pages of our site or files into the Nginx directory, dynamic page and database access are kept to the background of the Apache server.

C. The following two methods are introduced to achieve load balancing of server cluster.
We assume that the front-end Nginx (for 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), one root directory phpMyAdmin folder and test.php (inside the test code for print "Server1";), The other root directory simply places a test.php (inside the test code is the print "Server2";).

2 load balancing for different requests:

A. In the simplest way to build a reverse proxy (Nginx only handle static dynamic content, dynamic content to the background of the Apache server to deal with), we specifically set to: in the nginx.conf modified:
Copy Code code as follows:

Location ~ \.php$ {
Proxy_pass 158.37.70.143:80;
}

This way, when the client accesses the localhost:8080/index.html, the front end of the nginx will automatically respond;
When the user accesses the localhost:8080/test.php (this time the Nginx directory does not have the file at all), but through the above settings location ~ \.php$ ( Indicates that a regular expression matches a file that ends with. PHP, details see how location defines and matches the Http://wiki.nginx.org/NginxHttpCoreModule), Nginx server will automatically pass to 158.37.70.143 's Apache server. The test.php of the server is automatically parsed, then the HTML results page is returned to Nginx and Nginx is displayed (if Nginx uses memcached modules or squid to support caching), the output is printed server2.

As above is the simplest example of using nginx as a reverse proxy server;

B. We are now extending the example above to support two servers as above.
We set the nginx.conf part of the server module and modify the corresponding section to:
Copy Code code as follows:

Location ^~/phpmyadmin/{
Proxy_pass 127.0.0.1:80;
}
Location ~ \.php$ {
Proxy_pass 158.37.70.143:80;
}

The first section above location ^~/phpmyadmin/, which means that regular expression matching (^~) is not used, but rather a direct match, that is, if the URL that the client accesses is started with a http://localhost:8080/phpMyAdmin/( There is no phpMyAdmin directory at all in the local nginx directory, Nginx automatically pass to the 127.0.0.1:80 Apache server, which resolves the pages in the phpMyAdmin directory and sends the results to Nginx, which shows;
If the Client access URL is http://localhost/test.php, it will be processed by pass to 158.37.70.143:80 Apache.

Therefore, we realize the load balance for different requests.
If the user accesses the static page index.html, the front-end nginx directly responds;
If the user accesses the test.php page, 158.37.70.143:80 's Apache responds;
If the user accesses the page under the directory phpMyAdmin, 127.0.0.1:80 Apache responds;

3 Load Balancing to access the same page:
That is, when the user visits http://localhost:8080/test.php this same page, we realize the load balance of the two servers (in fact, the data on both servers require synchronization, Here we define the print Server1 and the Server2 respectively for the purpose of identifying the difference.

A. Now our situation is that under Windows Nginx is localhost listening 8080 ports;
Two Apache, one is 127.0.0.1:80 (contains the test.php page but print server1), the other is the 158.37.70.143:80 of the virtual machine (contains test.php page but print server2).

B. So reconfigure nginx.conf as:
is first added to the HTTP module of the Nginx profile nginx.conf, the server cluster cluster (where we are two) is defined:
Copy Code code as follows:

Upstream Mycluster {
Server 127.0.0.1:80;
Server 158.37.70.143:80;
}

Indicates that this server cluster contains 2 servers
And then defined in the server module, load Balancing:
Copy Code code as follows:

Location ~ \.php$ {
Proxy_pass Http://myCluster; #这里的名字和上面的cluster的名字相同
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;
}

In this case, if you visit the http://localhost:8080/test.php page, the Nginx directory does not have the file, but it will automatically pass it to the mycluster definition of the service cluster, respectively, by 127.0.0.1:80; Or 158.37.70.143:80, to do the processing.
The above does not have a weight defined after each server when defining upstream, indicating a balance between the two; if you want a more response, for example:
Copy Code code as follows:

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,1/6 access to the second. You can also define parameters such as Max_fails and Fail_timeout.
Http://wiki.nginx.org/NginxHttpUpstreamModule

In summary, we use the Nginx reverse proxy server to reverse the function of proxy server to the front of multiple Apache servers.
Nginx is only used to handle static page response and dynamic Request Agent pass, the background of Apache server as the app server to the foreground pass over the dynamic page processing and return to Nginx.

Through the above framework, we can realize the load balance of nginx and multiple Apache cluster cluster.
Two kinds of equilibrium:
1 can be defined in Nginx to access different content, proxy to different background server, as in the example of the Access phpMyAdmin directory agent to the first server, access to the test.php proxy to the second server;
2 can be defined in the Nginx to access the same page, the balance (of course, if the performance of the server can be defined to balance the weight) to the different background server. The example above accesses the test.php page, which is evenly represented on the server1 or Server2.
In practical application, the same app program and data are reserved on Server1 and Server2, which need to consider the data synchronization of both.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.