Nginx a powerful Web server for load Balancing

Source: Internet
Author: User
Tags nginx server phpmyadmin nginx reverse proxy

< turn >nginx This lightweight, high-performance Web server can do two things:
Directly as HTTP server (in place of Apache, requires fastcgi processor support for PHP);
Another feature is load balancing as a reverse proxy server

Here's an example of how to use Nginx for load balancing. Because Nginx has the advantage of dealing with concurrency, this application is now very common. Of course, Apache's mod_proxy and Mod_cache can also be used to implement reverse proxy and load balancing for multiple app servers, but Apache does not have the nginx expertise to handle concurrency.

1) Environment:

A. We are locally a Windows system and then use Virutalbox to install a virtual Linux system.
Install Nginx (listen on port 8080) and Apache (listen for 80 ports) on your local Windows system. Install Apache on a virtual Linux system (listening on port 80).
So we have 1 nginx in front as the reverse proxy server, and 2 Apache as the application server (can be considered as a small server cluster. ;-) ) ;

B. Nginx is used as a reverse proxy server, placed before two Apache, as a user access to the portal;
Nginx only handles static pages, and dynamic pages (PHP requests) are all delivered to two Apache in the background for processing.
In other words, the static pages or files of our website can be placed in the Nginx directory, and the dynamic pages and database access are reserved to the Apache server in the background.

C. The following two methods are introduced to implement server cluster load balancing.
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), a root directory to place phpMyAdmin folders and test.php (inside the test code for print "Server1";), The other root directory simply places a test.php (the test code for the print "Server2";).

2) load balancing for different requests:

A. In the simplest way to build the reverse proxy (Nginx only handles static non-processing dynamic content, dynamic content to the background of the Apache server to handle), we are specifically set to: in the nginx.conf modified:
Copy the 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 Nginx will respond automatically;
When the user accesses 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 the regular expression matches the file ending with. php, details see how location is defined and matched Http://wiki.nginx.org/NginxHttpCoreModule), and the Nginx server will automatically pass 158.37.70.143 's Apache server. The server test.php will be automatically parsed, and then return the HTML results page to Nginx, and then the Nginx display (if nginx use memcached module or squid can also support cache), the output is printed server2.

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

B. We now extend the example above to support two servers, such as the one above.
We set the Server Module section of nginx.conf to modify the corresponding section to:
Copy the 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 no regular expression matching (^~) is used, but rather a direct match, that is, if the URL that the client accesses is the beginning of the http://localhost:8080/phpMyAdmin/( The local Nginx directory does not have a phpmyadmin directory at all, Nginx will automatically pass to the 127.0.0.1:80 Apache server, the server to the phpMyAdmin directory of the page to parse, and then send the results to Nginx, the latter display;
If the Client access URL is http://localhost/test.php, it will be processed by the Apache pass to 158.37.70.143:80.

Therefore, we have implemented load balancing for different requests.
If the user accesses the static page index.html, the most front-end Nginx responds directly;
If the user accesses the test.php page, 158.37.70.143:80 Apache responds;
If the user accesses the page under directory phpMyAdmin, 127.0.0.1:80 Apache responds;

3) Load balancing to access the same page:
That is, when the user accesses http://localhost:8080/test.php this same page, we realize the load balancing of the two servers (in fact, the data on the two servers is consistent, Here we define the print Server1 and Server2 separately for the purpose of identifying the difference).

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

B. Therefore reconfigure the nginx.conf to:
First in the Nginx configuration file nginx.conf HTTP module, server Cluster server cluster (we are here are two) definition:
Copy the code code as follows:

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

Indicates that the server cluster contains 2 servers
Then defined in the server module, load Balancing:
Copy the 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 defined service area cluster, respectively, by 127.0.0.1:80; Or 158.37.70.143:80, to do the work.
Above the definition of the upstream, each server does not define a weight, indicating a balance between the two, if you want some more responses such as:
Copy the 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 to access the first SERVER,1/6 access to the second. In addition, parameters such as Max_fails and fail_timeout can also be defined.

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

Through the above architecture, we can realize the load balance of nginx and multiple Apache cluster cluster.
Two types of equalization:
1) can be defined in Nginx to access different content, proxy to a different background server, as in the example above to access the phpMyAdmin directory agent to the first server, access to the test.php agent on the second server;
2) can be defined in Nginx to access the same page, balanced (of course, if the server performance can be defined by the weight of a balanced) agent to different background server. As the example above accesses the test.php page, it will be evenly represented on the server1 or Server2.
In practice, the same app and data are kept on Server1 and Server2, and data synchronization between the two needs to be considered.

Nginx a powerful Web server for load Balancing

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.