First review the LB cluster load Balancer cluster
Four layers:
Lvs
Nginx (Stream)
Haproxy (MODE_TCP)
Seven floor
Http protocol
Nginx (Http,upstream)
Haproxy (Mode HTTP)
Httpd/ats/perlbal/pound/...
Next, how to implement nginx load balancing in HTTP
The Ngx_stream_proxy_module module can dispatch the HTTP service, where the stream module has
Specialized server subcommands, unlike other servers, which are used to define virtual hosts
While the server in the stream module is used to define one server in the group, the server can be reused multiple times,
Define multiple servers, so server load balancing can be achieved.
#################################################################
1, the experimental environment preparation, at least three host, one of which as Nginx Dispatch server, equipped with two network cards
Configure NGINX,HTTPD and httpd on three hosts, and test for successful access to the page
[Email protected] nginx]# Curl http://172.18.10.10:80/test1.html
Test Page 1 on UpStream Server 1 (172.18.10.10)
[Email protected] nginx]# Curl http://172.18.10.11:80/test1.html
Test Page 1 on UpStream Server 2 (172.18.10.11)
Make 172.18.10.10 and 172.18.10.11 a dynamic site (install httpd+php, i.e. Ap,listen 80)
172.18.10.10 and 172.18.10.11 as static sites (where 10.11 installs Nginx, monitor 8080,10.10 Configure virtual host, monitor 80 and 8080)
2, the purpose of the experiment. Implementation of Nginx load balancing for static content and dynamic content
3. Start configuration operation
Edit the PHP page under 172.18.10.10
[Email protected] ~]# vim/var/www/html/index.php
<H1>HTTPD Listend on server1
<?php
Phpinfo ();
?>
Send the experiment page to the 172.18.10.11 page file storage path
[Email protected] ~]# scp/var/www/html/index.php 172.18.10.11:/var/www/html/
Modify Server1 to Server2
<H1>HTTPD Listend on server2
<?php
Phpinfo ();
?>
4, common sense use Google browser request two address, to see if the test page can display properly--------tested found to be able to display the normal
5, configure the static site Nginx
SCP of the prepared Nginx packages to two hosts respectively
[Email protected] ~]# SCP nginx-1.6.2-1.el6.ngx.x86_64.rpm 172.18.10.10:/root/
6. Installing Nginx
[email protected] ~]# Yum install nginx-1.6.2-1.el6.ngx.x86_64.rpm
7. Configure virtual services for static sites
On 172.18.10.10:
Note DocumentRoot Path
#DocumentRoot "/var/www/html"
Add a new Listening port
#Listen 12.34.56.78:80
Listen 80
Listen 8080
Add a virtual host, listening on ports 80 and 8080, respectively
<virtualhost *:80>
Documentroot/var/www/shope
ServerName www.magedu.com
</VirtualHost>
<virtualhost *:8080>
Documentroot/var/www/html
ServerName imgs.magedu.com
</VirtualHost>
Save exit
Create a Directory
[Email protected] ~]# Mkdir/var/www/shope
Move index.php to this directory
[Email protected] ~]# mv/var/www/html/index.php/var/www/shope/
Check syntax
[Email protected] ~]# httpd-t
Restart HTTPD Service
[Email protected] ~]# service httpd restart
stopping httpd: [OK]
Starting httpd:httpd:Could not reliably determine the server ' s fully qualified domain name, using Localhost.localdomain For ServerName
[OK]
Access test 80 and port 8080 separately on the browser side
Results are normal.
On 172.18.10.11:
[Email protected] ~]# vim/etc/nginx/conf.d/default.conf
Change the virtual host listening port to 8080
Listen ———————------"Listen 8080;
Change the root path
root/usr/share/nginx/html, ————— "root/data/html;
Create a virtual Host directory path
[Email protected] ~]# MKDIR/DATA/HTML-PV
mkdir:created directory '/data '
mkdir:created directory '/data/html '
Move all test files to the/data/html directory
[Email protected] ~]# mv/var/www/html/test*/data/html/
Start the Nginx service and see if the port listens
[Email protected] ~]# Nginx
[Email protected] ~]# SS-TNL
State recv-q Send-q Local Address:port Peer Address:port
LISTEN 0 128 *:8080 *:*
LISTEN 0 128::: 80 :::*
LISTEN 0 128::: 22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100:: 1:25 :::*
LISTEN 0 100 127.0.0.1:25
Visit the page. See if you can access it properly
8. Nginx Service on the 172.18.200.100 of the Nginx Dispatch end
[Email protected] ~]# vim/etc/nginx/nginx.conf
#使用默认的加权轮询算法, the binding will be made
Upstream Websrvs {
Server 172.18.10.10:80 weight=2 max_fails=2 fail_timeout=2;
Server 172.18.10.11:80 weight=3;
}
Upstream Staticsrvs {
Server 172.18.10.10:8080 weight=1;
Server 172.18.10.11:8080 weight=1;
}
9, edit the scheduling method
[Email protected] ~]# vim/etc/nginx/conf.d/default.conf
Index index.php index.html; # # # #全局定义, sequencing
Location/{
Proxy_pass Http://websrvs; # # #动态资源加载路径定义
root/usr/share/nginx/html;
Index index.php index.html index.htm;
}
Location ~* \. (jpg|jpeg|png|gif|html) $ {
Proxy_pass Http://staticsrvs; # # # #静态资源加载路径定义
Index index.php;
}
10. New Load Test
[Email protected] ~]# nginx-t
Nginx:the configuration file/etc/nginx/nginx.conf syntax is OK
Nginx:configuration file/etc/nginx/nginx.conf Test is successful
[Email protected] ~]# Nginx-s Reload
Open Google Browser, enter the Http://172.18.200.100/Refresh page found will have the following page content to switch back and forth
HTTPD Listend on Server2
HTTPD Listend on Server1
Request http://172.18.200.100/index.php, found also the following page content switch back and forth
HTTPD Listend on Server2
HTTPD Listend on Server1
Use curl from other clients to test
[[email protected] ~]# for ((i=1;i<=10;i++));d o Curl http://172.18.200.100/index.php; Done
Test results: Load balancing for dynamic content
Next request static file: Http://172.18.200.100/test1.html, constantly refreshed, found to have the following to switch back and forth
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Use curl from other clients to test
[[email protected] ~]# for ((i=1;i<=10;i++));d o Curl http://172.18.200.100/test1.html; Done
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test Page 1 on UpStream Server 1 (172.18.10.10)
Test Page 1 on UpStream Server 2 (172.18.10.11)
Test result: To achieve dynamic content of the responsible balance
The result is static and dynamic separation, and we can define the cache to improve the efficiency.
This article is from the "12866758" blog, please be sure to keep this source http://12876758.blog.51cto.com/12866758/1925249
Experiment: Deploy and implement the static and dynamic separation and load balancing of Nginx on HTTP