码迷,mamicode.com
首页 > 其他好文 > 详细

nginx动静分离

时间:2016-04-25 16:35:42      阅读:957      评论:0      收藏:0      [点我收藏+]

标签:nginx 动静分离

  • 网站架构图

技术分享


环境说明:

nginx master192.168.232.18/24         Centos7

nginx backup192.168.232.19/24Centos7

 

webserver01(动态):192.168.232.30/24Centos7

webserver02(动态):192.168.232.31/24Centos7

 

webserver03(静态):192.168.232.6/24Centos6.7

webserver04(静态):192.168.232.254/24Centos6.7

如上图,nginx将访问php等动态页面的请求交给动态页面服务器;将访问html等静态页面的请求交给静态页面服务器。

  • 安装步骤

(1)  安装Keepalived

a)  master配置

安装依赖:

# yum installgcc gcc-c++ autoconf automake -y

# yum install zlibzlib-devel openssl openssl-devel -y

# tar xvf  keepalived-1.2.20.tar.gz -C /usr/local/src ;cd /usr/local/src/keepalived-1.2.20

# ./configure--prefix=/usr/local/keepalived

#make &&make install

#ln -s/usr/local/keepalived/sbin/keepalived /sbin/

# ln -s/usr/local/keepalived/bin/genhash /bin/

# ln -s /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/keepalived

# mkdir /etc/keepalived/

# cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/  

# cp/usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/

# servicekeepalived start

# chkconfigkeepalived on

 

#vim /etc/keepalived/keepalived.conf

 

vrrp_instanceVI_1 {                    #vrrp实例定义

stateMASTER                            #实例初始化状态,还可以是master!

nopreempt                               #不抢占,用于state 状态中,而且优先级要高于第二个backup

interface eth0                         #实例绑定的网卡

 

#track_interface{            #设定额外监控的网卡,以下任意网卡故障,状态fault

#eth0

#eth1

#}

 

virtual_router_id51                    #虚拟路由id0-255

priority100                            #优先级,高优先级的将竞选为MASTER

advert_int1                            #检查间隔,默认1s

 

authentication {

auth_typePASS                          #认证方式,pass

auth_passVI_1                          #认证密码

}

 

 

virtual_ipaddress{                     #虚拟ip地址(vip 可以为多个)

192.168.232.20/24eth0 cope global     #虚拟ip地址 绑定在 eth0 网卡

}

 

}

 

 

 

b)  backup配置

keepalived安装方式同master

#vim /etc/keepalived/keepalived.conf

 

 

vrrp_instanceVI_1

state BACKUP

#nopreempt

interface eth0

 

#track_interface{

#eth0

#eth1

#}

 

virtual_router_id51

priority 50

advert_int 1

 

authentication {

auth_type PASS

auth_pass VI_1

}

 

 

virtual_ipaddress{

192.168.232.20/24eth0 cope global

}

 

}

        

(2)安装nginx

  1. 解决pcre依赖

# tar xvf pcre-8.37.tar.bz2-C /usr/local/src/

  1. 编译安装nginx

[root@master ~] #tar xvf nginx-1.9.4.tar.gz -C  /usr/local/src/ ; cd/usr/local/src/nginx-1.9.4

[root@master ~] #./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--with-pcre=/usr/local/src/pcre-8.37

[root@master ~] # make ; make install ; cd

[root@master ~] # useradd -M -u 8001 -s /sbin/nologin nginx

[root@master ~] # vim /usr/local/nginx/conf/nginx.conf

      

[root@master ~] #/usr/local/nginx/sbin/nginx

[root@master ~] #echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

 

[root@master ~] #vim/usr/local/nginx/conf/nginx.conf

user nginx nginx;

worker_processes  auto;

 

events {

   worker_connections  65000;

}

 

http {

   include       mime.types;

   default_type application/octet-stream;

 

   sendfile        on;

 

   keepalive_timeout  65;

 

   server {

       listen       80;

       server_name  localhost;

 

        location / {

           root   html;

           index  index.html index.htm;

          

             if ($request_uri  ~*  \.(php|jsp)$){

                   proxy_pass http://phpservers;

             }

 

                   if($request_uri  ~*  \.(htm|html|shtml)$){

                   proxy_pass http://htmlservers;

             }

 

       }

 

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   html;

       }

 

    }

 

 

   upstream htmlservers {             #定义负载均衡服务器组名称

       server 192.168.232.254:80;

                   server192.168.232.6:80;

    }  

   upstream phpservers {

       server 192.168.232.30:80;

       server 192.168.232.31:80;

    }

}

 

以上安装nginx步骤在masterbackup上相同

 

测试配置文件有无错误,如果测试通过再重新加载配置文件。

[root@master ~] #/usr/local/nginx/sbin/nginx-t  

[root@master ~] #/usr/local/nginx/sbin/nginx-s reload

 

 

(3)静态文件服务器配置

 

[root@webserver01~] # yum install -y httpd

[root@webserver01~] #echo"webserver01" > /var/www/html/index.html

 

[root@webserver02~] # yum install -y httpd

[root@webserver02~] #echo"webserver02" > /var/www/html/index.html

 

 

(4)动态文件服务器配置

 

[root@webserver03~] # yum install -y httpdphp

[root@webserver03~] # echo"webserver03 <?php phpinfo(); ?>" > /var/www/html/index.php

 

 

[root@webserver04~] # yum install -y httpdphp

[root@webserver04~] # echo "webserver04<?php phpinfo(); ?>" > /var/www/html/index.php

 

以上是动静分离实验的相关配置。

 

(5)动静分离的写法样本

if ($request_uri ~*\.(html|gif|jpg|jpeg|ico|swf|htm|xml)$){

  proxy_pass http://squid_servers;

   }

if ($request_uri ~* \.(php|phps)$){

  proxy_pass http://apache_servers;

   }

if ($request_uri ~* \.(jsp|do)$){

  proxy_pass http://tomcat_servers;

   }

  proxy_pass http://localhost;

}

 

upstream tomcat_servers { s

    server  192.168.1.2:8080; 

    server  192.168.1.1:8080; 

    server  192.168.1.11:8080;

    }

upstream apache_servers {

   server  192.168.1.5:80; 

   server  192.168.1.177:80; 

   server  192.168.1.15:80;

    }

upstream squid_servers {

   server  192.168.1.26:3128; 

   server  192.168.1.55:3128; 

   server  192.168.1.18:3128;

}


nginx动静分离

标签:nginx 动静分离

原文地址:http://1011779.blog.51cto.com/1001779/1767525

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!