nginx動靜分離

來源:互聯網
上載者:User

標籤:nginx 動靜分離

  • 網站架構圖

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/7F/67/wKioL1cdvcSSxGRxAAEIQutaeRA466.jpg" title="繪圖1.jpg" alt="wKioL1cdvcSSxGRxAAEIQutaeRA466.jpg" />


環境說明:

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]er01~] #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動靜分離

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.