nginx负载均衡LAMP及基于memcached实现php会话保存
逻辑架构图:实验准备:
虚拟机: 172.18.250.75 nginx反代 keepalived高可用
虚拟机: 172.18.250.76 nginx反代 keepalived高可用
虚拟机: 172.18.250.77 LAMP memcached服务
虚拟机: 172.18.250.78 LAMP 共享memcached
实验要求: 两台nginx反向代理服务实现负载均衡,并实现高可用,两台后端服务器提供web服务,php会话保存在memcached中,实现会话共享。
一:nginx高可用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm 两台后端都安装nginx
warning: nginx-1.8.0-1.el6.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ###########################################
1:nginx ###########################################
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
]# yum -y install keepalived 两台后端都安装keepalived
编辑keepalived配置文件:实现双主模型
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
vrrp_script chk_nginx_down {
script "killall -0 nginx"
weight -5
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 151
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.250.99 dev eth0 label eth0:0
}
track_script {
ck_down
chk_nginx_down
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 152
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
172.18.250.100 dev eth0 label eth0:1
}
track_script {
ck_down
chk_nginx_down
}
}
启动keepalived的,查看是否实现双主模式
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
]# ifconfig
eth0 Link encap:EthernetHWaddr 00:0C:29:C5:A4:6B
inet addr:172.18.250.75Bcast:172.18.255.255Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fec5:a46b/64 Scope:Link
UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
RX packets:199576 errors:0 dropped:0 overruns:0 frame:0
TX packets:15786 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:23440249 (22.3 MiB)TX bytes:3193410 (3.0 MiB)
eth0:0 Link encap:EthernetHWaddr 00:0C:29:C5:A4:6B
inet addr:172.18.250.99Bcast:0.0.0.0Mask:255.255.255.255
UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
]# ifconfig
eth0 Link encap:EthernetHWaddr 00:0C:29:F6:9E:DF
inet addr:172.18.250.76Bcast:172.18.255.255Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fef6:9edf/64 Scope:Link
UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
RX packets:5448 errors:0 dropped:0 overruns:0 frame:0
TX packets:202 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:670054 (654.3 KiB)TX bytes:19959 (19.4 KiB)
eth0:1 Link encap:EthernetHWaddr 00:0C:29:F6:9E:DF
inet addr:172.18.250.100Bcast:0.0.0.0Mask:255.255.255.255
UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
二、配置nginx,实现负载均衡
1
2
3
4
5
6
7
8
9
]# vim /etc/nginx/nginx.conf
upstream webserver {
server 172.18.250.77:80 weight=1;
server 172.18.250.78:80 weight=1;
}
location / {
proxy_pass http://webserver;
indexindex.html index.htm;
}
启动nginx服务
三、配置后端服务器
1
2
3
4
]# yum -y install httpd php php-mysql
编辑测试页:
]# echo "Hello RS1" >index.html
]# echo "Hello RS2" >index.html
启动httpd服务,测试下主页:
测试能否负载均衡:
1
2
3
4
5
6
7
8
]# curl
Hello RS2
]# curl http://172.18.250.99
Hello RS1
]# curl http://172.18.250.100
Hello RS2
]# curl http://172.18.250.100
Hello RS1
测试能否请求php测试页:
四:配置php应用
1
2
3
4
5
]# mount -t nfs 172.18.250.77:/var/www/html/ /var/www/html/
250.78挂载250.77的nfs,,实现数据共享
]# cd /var/www/html
]# ]# unzip phpwind_UTF8_8.7
]# yum -y install mariadb-server /250.78上安装mysql,两台后端服务器共用一个mysql
刷新页面:
没有图片。。。。。。。。
编辑配置反向代理上的nginx配置文件:
1
2
3
4
5
6
7
8
9
//在location中添加下面四个语句:
proxy_next_upstream error timeout invalid_header http_500 http_502 http_504;
//如果后端返回的是500,502,504状态码的错误时,调度到下一台后端主机
proxy_set_header Host $host;
//设置主机host,因为nginx作为反向代理使用,而如果后端服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败。
proxy_set_header X-Real-IP $remote_addr;
//后端获取客户端的IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
//一并添加代理服务器IP和客户端IP到后端
在此刷新:显示OK
五:配置memcached缓存
1
2
]# yum -y install memcached php-pecl-memcached
]# service memcached start
配置php将会话保存至memcached中,编辑php.ini文件,确保如下两个参数的值分别如下所示:
1
2
session.save_handler = memcache
session.save_path = "tcp://172.16.200.11:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
重启apache服务,查看memcached有没有加载成功:
然后多次刷新,查看session有没有写入memcached中:
1
2
3
]# memcached-tool 127.0.0.1:11211
#Item_SizeMax_age Pages Count Full?Evicted Evict_Time OOM
1 96B 1009s 1 1 yes 0 0 0
nginx反向代理时,php应用可能出现乱码的情况,只需在nginx配置中加入charset utf-8就行。
六:为nginx反向代理前端开启缓存,减少对后端主机的压力:
1
2
3
4
5
proxy_cache_path /var/cache/nginx/proxy levels=2:1 keys_zone=pxcache:10m; 定义在http中
proxy_cache pxcache; 定义在location中
proxy_cache_key $request_uri;
proxy_cache_valid 200 301 302 10m;
proxy_cache_valid 403 404 5m;
重启,刷新,看缓存有没有生成:
1
2
3
4
5
]# ls
000f15282c393c434c545e667b7f8b8e9297a6abb4bbc7f5fc
02141e292e3a40474d595f717c858c8f93a2a9adb7c6e2f9fd
]# pwd
/var/cache/nginx/proxy
页:
[1]