In the daily development of network data, we have a high demand for the processing capacity of the server, but in the case of limited server, how to better utilize the server resources, so that our server to maximize their role? Load Balancing is a good way to do this.
What is nginx load balancing?
Nginx is a lightweight, high-performance webserver, he can mainly do the following two things:
(1). As an HTTP server (as with Apache)
(2). Load Balancing as a reverse proxy server
Now Nginx everywhere can be seen, often see the page after the outage will show Nginx words, which also shows that nginx because of high performance, use configuration simple, open source single These features are more and more users accept, used. One of the first as an HTTP server, combined with the PHP-FPM process, the request sent to the processing, nginx itself will not parse PHP, he just as a server, accept the request from the client, if it is a PHP request, then to the PHP process processing, and send the results of PHP processing to the client. This is very simple, install the NGINX+PHP-FPM after the configuration of the respective configuration file, start can be achieved. The principle of operation can be seen in the following paragraph explanation:
Nginx does not support direct invocation or parsing of external programs, and all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket under Linux (the socket can be either a file socket or an IP socket). In order to invoke a CGI program, you also need a fastcgi wrapper (wrapper can be understood as the program used to start another program), which is bound to a fixed socket, such as a port or a file socket. When Nginx sends the CGI request to the socket, through the FastCGI interface, the wrapper receives the request, and then derives a new thread, which invokes the interpreter or the external program to process the script and reads the return data; The wrapper then passes the returned data through the FastCGI interface, passing it to nginx along a fixed socket, and finally, Nginx sends the returned data to the client. This is the whole process of nginx+fastcgi, as shown in 1.
Figure 1 nginx+fastcgi Operation diagram
The above paragraph explains the nginx+fastcgi operation mechanism, in the Nginx configuration file will match the request, and make the corresponding processing, such as directly return the error file (here and above said a little difference, I think it is nginx internal to the HTML and other static files can do a similar resolution, the PHP process to use the PHP request processing (here the PHP process can be multiple).
The second is to use the reverse proxy load balancing, this is actually very simple, that is, to define a set of servers, to match the request, and transfer the request to any of the server processing, to alleviate the pressure of each server, first look at the online definition of the reverse proxy:
The reverse proxy method refers to a proxy server that accepts connection requests on the Internet, then forwards the request to a server on the internal network and returns the results from the server to the client requesting the connection on the Internet, Reverse. At this point the proxy server appears as a reverse proxy server externally.
Figure 2 Nginx Load Balancing
Reverse proxy is the opposite of the proxy (or proxy), you have to listen to the proxy, in order to more convenient access to B resources, through a resource indirect access to B resources, the characteristics of users know what the final site to visit is what, but the reverse proxy users do not know what to do behind the proxy server, Reverse proxy in the service of the real processing server in the intranet, the external network can only access the reverse proxy server, which greatly improves security.
Installing the Software
Nginx installation is simple
1, install Nginx needs environment, pcre (function rewrite), zlib (function compression), SSL, this can also download the compilation installation
Yum-y install zlib;
Yum–y install pcre;
Yum–y install OpenSSL;
2. Download and install nginx-*.tar.gz.
TAR–ZXVF nginx-1.2.8.tar.gz–c./;
CD nginx-1.2.8;
./congigure--prefix=/usr/local/nginx;
Make && make install;
3. Configuration
This configuration only needs to modify the content between http{}, the first place to modify is to set up a server group, between the HTTP nodes to add
Upstream myserver{
Server www.abc.com:80; #这里是你自己要做负载均衡的服务器地址1
Server www.hao.com:8080; #这里是要参与负载均衡的地址2
}
The upstream in Nginx supports the following methods: polling (by default, one access to all servers in chronological order, automatically culling if a server goes down), weight (the server's azimuth probability is proportional to the weight, This can be configured when the server configuration is uneven, ip_hash (hash of each request IP, and according to certain rules assigned to the corresponding server), fair (according to each server response time (RT) To allocate the request, RT know priority allocation), Url_ Hash (according to the hash value of the access URL to allocate the request), I use the default rotation method here.
Point the request to MyServer
Location/{
Proxy_pass Http://myServer;
}
The complete file (delete comment) is as follows:
Worker_processes 1;
Events {
Worker_connections 1024;
}
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
Upstream myserver{
Server www.hao.com:80;
Server www.abc.com:8080;
}
server {
Listen 80;
server_name my22;
Location/{
Proxy_pass Http://myServer;
}
}
}
Set up the reverse proxy backend as a load balancer of two servers
You can see the previous step has two server addresses, www.hao.com:80 and www.abc.com:8080, above the Nginx I was installed on the virtual machine above, these two servers I was installed in the native WIN8 system, Using Apache's VirtualHost, set up two domain names, the code under the two domain names is independent of each other, and the settings are simple:
1. Set Apache configuration file
I am using the XAMPP integration environment, there are two places to modify, add in the httpd.conf where the listening port is
Listen 8080
Which means this place is listening to two ports.
Listen 80
Listen 8080
See if the following sentence is open, not open, open, open as shown below
# Virtual Hosts
Include conf/extra/httpd-vhosts.conf
Add the following to the httpd-vhosts.conf
<virtualhost *:80>
ServerName www.hao.com #对应的域名, load-balanced server address
DocumentRoot E:\soft\xampp\htdocs\www.hao.com #代码文件夹
</VirtualHost>
<virtualhost *:8080>
ServerName www.abc.com
DocumentRoot E:\soft\xampp\htdocs\www.abc.com
</VirtualHost>
Modify the Hosts file for Windows and append the following:
127.0.0.1 www.hao.com
127.0.0.1 www.abc.com
Modify the Linux/etc/hosts file and append the following:
78.45.13.26 www.hao.com #这里前面的地址对应我win8本机的ip地址
78.45.13.26 www.abc.com
I put a file in www.hao.com:80. index.php "E:\soft\xampp\htdocs\www.hao.com\index.php"
www.abc.com:8080 also put a file index.php "E:\soft\xampp\htdocs\www.abc.com\index.php"
The contents of the file are basically the same, except that I'm the ABC of this place is different, one is Linux, the other is ABC.
If you can enter www.hao.com:80 and www.abc.com:8080 in the Win8 browser to see different effects
And below the CentOS see below the result (oneself beautify the next) explanation the configuration is successful
[Email protected] nginx]# Curl www.hao.com:80
I ' m The Linux<br> "view" 1
[Email protected] nginx]# Curl www.abc.com:8080
I ' m The Abc<br> "view" 1
<?php
Session_save_path ("./");
Session_Start ();
Header ("Content-type:text/html;charset=utf-8");
if (Isset ($_session[' view ')) {
$_session[' view ' = $_session[' view ' + 1;
}else{
$_session[' view ' = 1;
}
echo "I ' m The Abc<br>";
echo "View" {$_session[' view '} ";
Look at the effect
After all OK can be hard through browser access to see the effect
Forgot to say, the address of Nginx Proxy server is http://78.36.11.118,
After the browser input http://78.36.11.118/index.php, you will find that the
I ' m the ABC, I ' M the Linux
The two pages are exchanged back and forth, the view will not be refreshed twice, which also proves that the default is rotation, but there is a more common problem, when the user visited the site, did not handle the case, The session will be saved on a different server (I'll use two different folders to simulate two servers).
CentOS configuration Nginx Load Balancing detailed