Configuration diagram of Nginx Reverse proxy for load balancing

Source: Internet
Author: User
Tags regular expression nginx server phpmyadmin nginx reverse proxy
First, we will briefly introduce nginx as a reverse proxy to achieve load balancing.
The reverse proxy method is to use the reverse proxy server to accept connection requests on the Internet and then forward the requests to the servers on the internal network, return the result obtained from the server to the client requesting connection on the internet. The reverse proxy server is displayed as a server. Enable hosts on the internet to access different intranet host resources through different domain names, so as to protect intranet hosts from external network host attacks and implement load balancing and cache functions, to a large extent, how can we reduce the burden on web servers? Improves access speed.
 
A simple Nginx is used as a reverse proxy to achieve load balancing.
As shown in the preceding figure, I have created an environment with three computers (for windows systems, we recommend that you use a linux system ).
The environment is described as follows:
1. Nginx server 192.168.2.3. Install nginx as the reverse proxy server (port 80 ).
Install nginx + php 192.168.2.2 (port 80) on 2.1 computers as web server 1
Install apache + php 192.168.2.8 (port 80) on 3.1 computers as web Server 2
 
(1) load balancing for different requests.
Nginx only processes static pages, and all dynamic pages (php requests) are delivered to backend apache 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.
The following example is used to describe the HTML and php web pages. Test.html is under the nginx directory and test. php is under the apache Directory.
Modify the default nginx. conf, which is about 59 ~ Line 61: remove the # sign and restart nginx.
 
# Location ~ . Php $ {# proxy_pass http: // 127.0.0.1; #} changed to location ~ . Php $ {proxy_pass http: // 127.0.0.1: 8080 ;}

Access the server separately, as shown in the following figure.

 
In this way, when we access 192.168.2.3/index.html, the front-end nginx will automatically respond; when we access 192.168.2.3/test. in php (this file is not available in the nginx directory at this time), but through the above settings location ~ . Php $ (indicating that the regular expression matches. php at the end of the file, see the location is how to define and match, the official website document http://wiki.nginx.org/NginxHttpCoreModule), nginx server will automatically pass to 192.168.2.3 apache server. Test. php under the server will be automatically parsed, and the result page of test. php will be returned to nginx, and then nginx will be displayed.
 
 
An example of implementing load balancing for different requests by multiple servers:
Static page test.html, the front-end nginx responds directly;
Access the Apache test. php, 192.168.2.3: 8080 on the php page to respond;
When you access the page under phpMyAdmin, Apache at 192.168.2.2: 80 responds.
Modify the server module of the original default nginx. conf file (about 59 ~ 61 rows ):
# Location ~ . Php $ {# proxy_pass http: // 127.0.0.1; #} changed to location ^ ~ /PhpMyAdmin/{proxy_pass 192.168.2.2: 80;} location ~ . Php $ {proxy_pass 192.168.2.3: 8080 ;}
 
The first part above is location ^ ~ /PhpMyAdmin/, indicating that regular expressions are not used for matching (^ ~), Directly match, that is, if the client accesses a URL starting with http: // 192.168.2.3/phpMyAdmin/(The phpMyAdmin directory does not exist in the local nginx directory ), nginx automatically passes to the Apache server 192.168.2.2: 80, which parses the page under the phpMyAdmin directory and sends the result to nginx for display.
 
 
(2) server load balancer accessing the same page
When accessing the same page of http: // 192.168.2.3/test. php, we implement load balancing for the three servers (in actual situations, the data on the two servers must be synchronized ).
Reconfigure nginx. conf and use the default nginx. conf.
1. First, delete some configurations under the server, which may be 36 ~ 46 rows. The following configuration lines are deleted.
 
Listen 80; server_name localhost; # charset KOI8-R; # access_log logs/host. access. log main; location/{root html; index index.html index.htm ;}
2. Add the server cluster definition in the http module of the configuration file nginx. conf.
 
Upstream myCluster {server 192.168.2.3: 8080; server 192.168.2.2: 80; server 192.168.2.8: 80 ;}
Indicates that the server cluster contains three servers.
 
3. Define server load balancer in the server module.
 
Location ~ . Php $ {proxy_pass http: // myCluster; proxy_redirect off; proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ response ;}
 
Proxy_pass http: // myCluster; the name here is the same as the name of the above cluster
 
After configuration, when accessing http: // 192.168.2.3/test. on the php page, the nginx directory does not have this file at all, but it will automatically pass it to the server group defined by myCluster, which will be processed by one of the above three servers.
 
 
When upstream is defined above, no weight is defined after each server, indicating a balance between the two. If you want more responses, you can add weight.
Upstream myCluster {server 192.168.2.3: 8080 weight = 5; server 192.168.2.2: 80; server 192.168.2.8: 80 ;}
 
This means 5/7 of the probability of accessing the first server, 1/7 of the second and third. You can also define parameters such as max_fails and fail_timeout.
 
Therefore, we use 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. The apache server in the background processes the dynamic pages passed by the front-end and returns them to nginx.
In actual applications, each server retains the same program and data respectively, and data synchronization between the two needs to be considered.

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.