Nginx动静分离

  • 实验拓扑:

 nginx动静分离_第1张图片

  • 实验环境:

Nginx分发器 192.168.42.175 xuegod175.cn

Web1服务器 192.168.42.176 xuegod176.cn   作为p_w_picpath图片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 p_w_picpath  {
    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://p_w_picpath;
}
}
}
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/p_w_picpaths.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

从客户端进行访问测试

nginx动静分离_第2张图片

 说明nginx web1服务已经成功支持访问图片

通过分发器进行访问测试(图片文件是web1服务器的)

nginx动静分离_第3张图片

说明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   创建测试文件

[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访问测试:

nginx动静分离_第4张图片

说明177web 已经支持php 可以正常使用访问。

动静分离访问进行测试

nginx动静分离_第5张图片

 

访问nginx分发器 index.php(这个文件是177web2服务器上的),nginx分发器可以正常读取172 web2服务器的php 文件,说明读写分离已经成功。