1024programmer Nginx Nginx server configuration reverse proxy and load balancing

Nginx server configuration reverse proxy and load balancing

nginxReverse proxy load:

Environment: There is 1 nginx on the front end as a reverse proxy server (install nginx (listening to port 8080) and apache (listening to port 80)); there are 2 apaches behind it as application servers (listening to port 80)

1. Nginx is used as a reverse proxy server, placed in front of the two Apaches, as the entrance for user access;
Nginx only handles static pages, and all dynamic pages (php requests) are delivered to the two apaches in the background for processing.
That is to say, we can place the static pages or files of our website in the nginx directory; dynamic pages and database access are reserved on the apache server in the background.

2. There are two methods to achieve load balancing of server cluster. the

We assume that the front-end nginx (127.0.0.1:80) only contains a static page index.html;
There are two apache servers in the background (localhost:80 and 119.168.136.2:80 respectively), one root directory places the phpMyAdmin folder and test.php (the test code inside is print
“server1”;), ​​another root directory only places a test.php (the test code inside is print “server2”;).

1). The simplest example of using nginx as a reverse proxy server; when building a reverse proxy in the simplest way
(nginx only handles static and not dynamic content, and dynamic content is handed over to apache in the background
server), our specific setting is: modify in nginx.conf:
location ~ \.php$ {
proxy_pass 119.168.136.2:80 ;
}

a)
When the client visits localhost:8080/index.html, the front-end nginx will automatically respond;
b) When the user visits localhost:8080/test.php (there is no such file in the nginx directory at this time), but through the above settings
location ~ \.php$(indicates that the regular expression matches files ending with .php, see how location is defined and matched for details
http://wiki.nginx.org/NginxHttpCoreModule), the nginx server will automatically pass to
119.168.136.2 of the apache server. The test.php under the server will be automatically parsed, and then the html result page will be returned to nginx, and then
nginx to display (if nginx uses memcached module or squid can also support caching), the output result is print server2. the

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

The location of the first part above ^~
/phpMyAdmin/, means not using regular expression matching (^~), but direct matching, that is, if the client accesses
The URL is http://localhost:8080/phpMyAdmin/
At the beginning (there is no phpMyAdmin directory in the local nginx directory), nginx will automatically pass to 127.0.0.1:80
Apache server, which parses the pages under the phpMyAdmin directory, and then sends the results to nginx, which displays;
If the client access URL is http://localhost/test.php, it will be passed to 119.168.136.2:80
The apache for processing. the

So in summary, we have achieved load balancing for different requests.
〉If the user visits the static page index.html, the front-end nginx responds directly;
〉If the user visits the test.php page, Apache at 119.168.136.2:80 will respond;
〉If the user visits the pages under the directory phpMyAdmin, Apache at 127.0.0.1:80 will respond; 

3) Load balancing for accessing the same page:
That is, when a user visits the same page http://localhost:8080/test.php, we implement load balancing between the two servers
(In actual situation, the data on the two servers are required to be synchronized and consistent. Here we define printing server1 and server2 respectively for identification and distinction). the

a. The front-end nginx is localhost listening on port 8080;
Two apache, one is 127.0.0.1:80 (includes test.php page but prints server1), the other is 119.168.136.2:80 (includes test.php page but prints server2). the

b. So reconfigure nginx.conf as:
〉First add in the http module of the nginx configuration file nginx.conf, the server cluster server
The definition of cluster (we have two here):
upstream myCluster {
server 127.0.0.1:80 ;
server 119.168.136.2:80 ;
}

Indicates that this server cluster contains 2 servers
〉Then define in the server module, load balancing:
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 case, if you visit http://localhost:8080/test.php
For pages, the root of the nginx directoryThere is no such file, but it will automatically pass it to the cluster in the service area defined by myCluster, which is processed by 127.0.0.1:80; or 119.168.136.2:80; respectively.
When defining upstream above, there is no weight defined after each server, which means that the two are balanced; if you want more responses, for example:
upstream myCluster {
server 127.0.0.1:80 weight=5;
server 119.168.136.2:80 ;
}

This means that there is a 5/6 chance to visit the first server, and 1/6 to visit the second. In addition, parameters such as max_fails and fail_timeout can be defined. the

To sum up, we use the reverse proxy server function of nginx to deploy it to multiple apache
The front end of the server.
Nginx is only used to process static page responses and dynamic request proxy pass, and the background Apache server is used as app
The server processes the dynamic pages passed by the front desk and returns them to nginx. the

Through the above architecture, we can realize the load balancing of the cluster composed of nginx and multiple Apaches.
Two balances:
1) You can define access to different content in nginx and proxy to different background servers;
As in the above example, access to the phpMyAdmin directory is proxied to the first server; access to test.php is proxied to the second server;
2) It can be defined in nginx to access the same page, and evenly (of course, if the server performance is different, you can define the weight to balance) proxy to different background servers.
In the above example, when accessing the test.php page, it will be proxied to server1 or server2 in a balanced manner.
In practical applications, the same app program and data are reserved on server1 and server2 respectively, and data synchronization between the two needs to be considered.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/nginx-server-configuration-reverse-proxy-and-load-balancing/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索