nginx 动静分离

  网站架构图

      环境说明:

      nginx master:192.168.232.18/24         (Centos7)

      nginx backup:192.168.232.19/24(Centos7)

      webserver01(动态):192.168.232.30/24(Centos7)

      webserver02(动态):192.168.232.31/24(Centos7)

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

      webserver04(静态):192.168.232.254/24(Centos6.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                    #虚拟路由id(0-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

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

      [[email protected] ~] #./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

      [[email protected] ~] # make ; make install ; cd

      [[email protected] ~] # useradd -M -u 8001 -s /sbin/nologin nginx

      [[email protected] ~] # vim /usr/local/nginx/conf/nginx.conf

      [[email protected] ~] #/usr/local/nginx/sbin/nginx

      [[email protected] ~] #echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

      [[email protected] ~] #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步骤在master和backup上相同

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

      [[email protected] ~] #/usr/local/nginx/sbin/nginx-t  

      [[email protected] ~] #/usr/local/nginx/sbin/nginx-s reload

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

      [[email protected]~] # yum install -y httpd

      [[email protected]~] #echo"webserver01" > /var/www/html/index.html

      [[email protected]~] # yum install -y httpd

      [[email protected]~] #echo"webserver02" > /var/www/html/index.html

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

      [[email protected]~] # yum install -y httpdphp

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

      [[email protected]~] # yum install -y httpdphp

      [[email protected]~] # 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值