实验拓扑:
实验环境:
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实验配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
[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
从客户端进行访问测试
说明nginx web1服务已经成功支持访问图片
通过分发器进行访问测试(图片文件是web1服务器的)
说明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(); ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[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访问测试:
说明177web 已经支持php 可以正常使用访问。
动静分离访问进行测试
访问nginx分发器 index.php(这个文件是177web2服务器上的),nginx分发器可以正常读取172 web2服务器的php 文件,说明读写分离已经成功。
本文转自 Innocence_caosm 51CTO博客,原文链接:http://blog.51cto.com/innocence/1795551,如需转载请自行联系原作者