nginx動靜分離

Nginx動靜分離

  • 實驗拓撲:

 wKioL1kK6LeA1DHkAACmoj4h-8Y942.jpg-wh_50

  • 實驗環境:

Nginx分發器 192.168.42.175 xuegod175.cn

Web1服務器 192.168.42.176 xuegod176.cn   作爲image圖片web服務器

Web2服務器 192.168.42.177 xuegod177.cn   作爲php web服務器

 

實驗注意:關閉iptables selinux 配置好hosts 文件,兩臺web服務器這裏均使用了nginx

 

  •  實驗配置:

nginx實驗配置

[root@xuegod175 ~]# cat /usr/local/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes  1;
pid        logs/nginx.pid;
events {
worker_connections  1024;
}
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
 
#負責壓縮數據流
gzip              on;
gzip_min_length   1000;
gzip_types        text/plain text/css application/x-javascript;
 
upstream web {
    server  192.168.42.175 weight=1 max_fails=2  fail_timeout=2;
} 
upstream image  {
    server  192.168.42.176 weight=1 max_fails=2  fail_timeout=2;
} 
upstream php {
    server  192.168.42.177 weight=1 max_fails=2  fail_timeout=2;    
}
server {
       listen 80;
       server_name 192.168.42.175; 
location  / {
    root html/web;
    index  index.php index.html;
}
location ~* \.php$ {
   root html;
   proxy_pass http://php;
}
location ~* ".(jpg|png|jpeg|gif)" {
    proxy_pass http://image;
}
}
}
Web1服務器配置
[root@xuegod176 conf]# cat nginx.conf
worker_processes  1;
user nginx nginx ;
events {
    worker_connections  1024;
}
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;
        }
       location ~ .*\.(gif|jpg|jpeg|png)$ {    
        expires 24h;  
            access_log /usr/local/nginx/logs/images.log;    圖片日誌位置
            proxy_store on;    
            proxy_store_access user:rw group:rw all:rw;    
            proxy_redirect          off;    
            proxy_set_header        Host 127.0.0.1;    
            client_max_body_size    10m;    
            client_body_buffer_size 1280k;    
            proxy_connect_timeout   900;    
            proxy_send_timeout      900;    
            proxy_read_timeout      900;    
            proxy_buffer_size       40k;    
            proxy_buffers           40 320k;    
            proxy_busy_buffers_size 640k;    
            proxy_temp_file_write_size 640k;    
            }    
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

配置測試文件(支持圖片)

[root@xuegod176 html]# ls
50x.html  a  abc.jpg  bin  binbin.jpeg  index.html  index.htmlbak  nginx.conf

從客戶端進行訪問測試

wKiom1kK88jS7WlxAAB0G87uJho510.jpg-wh_50

 說明nginx web1服務已經成功支持訪問圖片

通過分發器進行訪問測試(圖片文件是web1服務器的)

wKioL1kK9DTjK5fNAACMWkYukiw178.jpg-wh_50

說明nginx讀寫分離已經成功  nginx分發器可以正常的讀取web1服務器的圖片文件

Web2服務器配置

爲了使nginx能夠支持訪問php環境,這裏特意安裝了lnmp環境
安裝mysql
[root@xuegod177 ]yum -y install mysql mysql-server mysql-devel
[root@xuegod177 ] /etc/init.d/mysqld start
安裝php
[root@xuegod177 ]yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml –y
[root@xuegod177 ]yum install php-fpm –y
[root@xuegod177 ]/etc/init.d/php-fpm start
[root@xuegod177 html]# ls
50x.html  index.html  index.html.bak  index.php
[root@xuegod177 html]# cat index.php   創建測試文件
<?php phpinfo(); ?>

[root@xuegod177 conf]# cat nginx.conf
user nginx nginx; 
worker_processes  1;
events {
    worker_connections  1024;
}
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;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;  使nginx 支持訪問php界面
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

Web2訪問測試:

wKiom1kK9SPwfAZnAAEhO1NZvDk055.jpg-wh_50

說明177web 已經支持php 可以正常使用訪問。

動靜分離訪問進行測試

wKioL1kK9YPjqpPNAAF4HNF3HjA104.jpg-wh_50

 

訪問nginx分發器 index.php(這個文件是177web2服務器上的),nginx分發器可以正常讀取172 web2服務器的php 文件,說明讀寫分離已經成功。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章