1. 程式人生 > >nginx實現動靜分離的負載均衡叢集

nginx實現動靜分離的負載均衡叢集

[[email protected] ~]#yum groupinstall "Development Tools" "Development Libraries" -y
[[email protected] ~]#yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* -y

[[email protected] ~]#tar -zxvf nginx-1.9.4.tar.gz -C /usr/local/src/
[[email protected] ~]#cd /usr/local/src/
[

[email protected] src]#cd nginx-1.9.4/
[[email protected] nginx-1.9.4]#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
 [[email protected] nginx-1.9.4]#make && make install
 [
[email protected]
nginx-1.9.4]#useradd -M -u 8001 -s /sbin/nologin nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx     啟動nginx
[[email protected] ~]# netstat -antup|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      42350/nginx
測試nginx

配置檔案:

  2 user  nginx nginx;
43         location / {
 44             root   html;
 45             index  index.html index.htm;
 46         if ($request_uri ~* \.html$){
 47                    proxy_pass http://htmlservers;
 48            }
 49         if ($request_uri ~* \.php$){
 50                    proxy_pass http://phpservers;
 51            }
 52                    proxy_pass http://picservers;
 53         }
123 upstream  htmlservers {   #定義負載均衡伺服器組名稱
124          server 192.168.199.4:80;
125          server 192.168.199.5:80;
126  }
127  upstream  phpservers{
128          server 192.168.199.4:80;
129          server 192.168.199.5:80;
130  }
131  upstream  picservers {
132          server 192.168.199.4:80;
133          server 192.168.199.5:80;
134  }
135 }
[[email protected] ~]# ln -s /usr/local/nginx/sbin/* /usr/bin/
[[email protected] ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] ~]# nginx -s reload
配置後端伺服器(tiandong64和tiandong65上面配置一樣):
[[email protected] ~]# yum install php httpd -y
[[email protected] ~]# yum -y install php httpd -y
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# more index.html
192.168.199.4
[[email protected] html]# more test.php
192.168.199.4
<?php
    phpinfo();
?>
[[email protected] html]#
[[email protected] html]# ll
total 12
-rw-r--r-- 1 root root  14 Mar 20 00:24 index.html
-rw-r--r-- 1 root root 790 Dec 24  2018 pic.PNG
-rw-r--r-- 1 root root  35 Mar 20 00:25 test.php
重啟Apache服務:
[[email protected] ~]# /etc/init.d/httpd restart
[[email protected] ~]# /etc/init.d/httpd restart
測試一下html頁面:
[[email protected] ~]# curl 192.168.199.3
192.168.199.5
[[email protected] ~]# curl 192.168.199.3
192.168.199.4
[[email protected] ~]# curl 192.168.199.3
192.168.199.5
[[email protected] ~]# curl 192.168.199.3
192.168.199.4
測試php頁面: