An example of implementing Load Balancing using nginx as a reverse proxy:
Reprinted please indicate the original link: http://blog.csdn.net/omohe/archive/2009/07/09/4335765.aspx Version: V1.0 Author: Omo last modification time: 2009.07.09 1) Environment: A. We use a local Windows system, and then use virutalbox to install a virtual Linux system. Install nginx (Listening to port 8080) and Apache (Listening to port 80) on the local Windows system respectively ). Install Apache on a virtual Linux system (Listening to port 80 ). In this way, we have one nginx server as the reverse proxy server at the front end, and two Apache servers as the application server (which can be considered as a small server cluster .; -)); B. nginx is used as the reverse proxy server. It is placed before two Apache servers and serves as the portal for user access; Nginx only processes static pages. Dynamic Pages (PhP requests) are all delivered to two backend Apache servers for processing. That is to say, we can place static pages or files on our website to the nginx directory; dynamic pages and database access are retained to the Apache server in the background. C. The following two methods are introduced to achieve Server Cluster load balancing. Let's assume that the front-end nginx (for 127.0.0.1: 8080)contains a static page index.html; The two backend Apache servers (localhost: 80 and 158.37.70.143: 80), and the phpMyAdmin folder and test. PHP (the test code is print "server1";), and the other root directory only contains one test. PHP (the test code is print "server2 ";). 2) Load Balancing for different requests: A. When building reverse proxy in the simplest way(Nginx only processes static and dynamic content, and the dynamic content is handed over to the Apache server in the background.) Our specific settings are as follows: Modify in nginx. conf: Location ~ /. Php $ { Proxy_pass 158.37.70.143: 80; } > In this way, when the client accesses localhost: 8080/index.html, the front-end nginx will automatically respond; > When the user accesses localhost: 8080/test. php (this file is not in the nginx directory at all), but the location ~ /. Php $ (indicates that the regular expression matches the file ending with. php, see how location defines and matches the http://wiki.nginx.org/NginxHttpCoreModule for details), The nginx server will automatically pass to the Apache server 158.37.70.143. Test. PHP will be automatically parsed, And the HTML result page will be returned to nginx, and then nginx will be displayed (if nginx uses the memcached module or squid, it can also support caching ), the output result is printed server2. The above is the simplest example of using nginx as the reverse proxy server; B. Now we can extend the above example to support the two servers above. We set the server module section of nginx. conf: Location ^ ~ /PHPmyAdmin /{ Proxy_pass 127.0.0.1: 80; } Location ~ /. Php $ { Proxy_pass 158.37.70.143: 80; } The first part above is location ^ ~ /PHPmyAdmin/, indicating that regular expressions are not used for matching (^ ~), It directly matches, that is, if the client accesses a URL starting with http: // localhost: 8080/PHPmyAdmin/(the phpMyAdmin directory is not in the local nginx directory ), nginx will automatically pass to the Apache server 127.0.0.1: 80, which parses the page under the phpMyAdmin directory, and then sends the result to nginx, which is displayed; If the client access URL is http: // localhost/test. php, it will be processed by Apache that passes to 158.37.70.143: 80. Therefore, we have achieved Load Balancing for different requests. > If the user crashes the static page index.html, The frontend nginx responds directly; > If the user accesses the test. PHP page, Apache of 158.37.70.143: 80 responds; > If the user accesses the page under phpMyAdmin, Apache of 127.0.0.1: 80 responds; 3) Server Load balancer accessing the same page: That isWhen you access the same page http: // localhost: 8080/test. php, we implement load balancing between the two servers.(In actual situations, the data on the two servers must be synchronized. Here we define printing server1 and server2 respectively to identify the differences ). A. Now, in windows, nginx is localhost listening for port 8080; Two Apache servers: 127.0.0.1: 80 (including the test. PHP page but printing server1) and 158.37.70.143: 80 (including the test. PHP page but printing server2 ). B. Therefore, reconfigure nginx. conf: > FirstAdd it to the HTTP module of nginx. conf in the nginx configuration file, and define the server cluster (two servers here) of the server cluster: Upstream mycluster { Server 127.0.0.1: 80; Server 158.37.70.143: 80; } Indicates that the server cluster contains two servers. > Then, define in the server module, Server Load balancer: Location ~ /. Php $ { Proxy_pass http: // mycluster; # The name here is the same as the name of the above 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 caseHttp: // localhost: 8080/test. PHP page, the nginx directory does not have this file, but it will automatically pass it to the service cluster defined by mycluster, which consists of 127.0.0.1: 80; or 158.37.70.143: 80; for processing. When upstream is defined above, no weight is defined after each server, indicating a balance between the two. If you want more responses, for example: Upstream mycluster { Server 127.0.0.1: 80 Weight = 5; Server 158.37.70.143: 80; } This means 5/6 of the probability of accessing the first server and 1/6 of the second. You can also define parameters such as max_fails and fail_timeout. Http://wiki.nginx.org/NginxHttpUpstreamModule ================================ To sum up, weUse the nginx reverse proxy server function to deploy it to the front ends of Multiple Apache servers. Nginx is only used to process the proxy pass for Static Page responses and dynamic requests. Apache server in the background serves as the app server to process the dynamic pages passed by the front-end and return them to nginx. With the above architecture, we can achieve Load Balancing for the cluster of nginx and Multiple Apache clusters. Two types of balancing: 1) You can define access to different content in nginx and proxy to different backend servers;In the above example, access the phpMyAdmin Directory Proxy to the first server; access the test. php proxy to the second server; 2) You can define in nginx to access the same page, balanced(If the server performance is different, you can define the weight for balancing)To different backend servers.In the preceding example, accessing the test. PHP page will evenly proxy to server1 or server2. In actual applications, the same app and data are retained on Server 1 and Server 2 respectively, so data synchronization between the two needs to be considered. |