nginx负载均衡LAMP及基于memcached实现php会话保存
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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高可用
]# 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... ########################################### [100%] 1:nginx ########################################### [100%] ---------------------------------------------------------------------- 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配置文件:实现双主模型
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的,查看是否实现双主模式
]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:C5:A4:6B inet addr:172.18.250.75 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fec5:a46b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric: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:Ethernet HWaddr 00:0C:29:C5:A4:6B inet addr:172.18.250.99 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 ]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:F6:9E:DF inet addr:172.18.250.76 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fef6:9edf/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric: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:Ethernet HWaddr 00:0C:29:F6:9E:DF inet addr:172.18.250.100 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
二、配置nginx,实现负载均衡
]# 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; index index.html index.htm; }
启动nginx服务
三、配置后端服务器
]# yum -y install httpd php php-mysql 编辑测试页: ]# echo "Hello RS1" >index.html ]# echo "Hello RS2" >index.html
启动httpd服务,测试下主页:
测试能否负载均衡:
]# 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应用
]# 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配置文件:
//在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缓存
]# yum -y install memcached php-pecl-memcached ]# service memcached start
配置php将会话保存至memcached中,编辑php.ini文件,确保如下两个参数的值分别如下所示:
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中:
]# memcached-tool 127.0.0.1:11211 # Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM 1 96B 1009s 1 1 yes 0 0 0
nginx反向代理时,php应用可能出现乱码的情况,只需在nginx配置中加入charset utf-8就行。
六:为nginx反向代理前端开启缓存,减少对后端主机的压力:
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;
重启,刷新,看缓存有没有生成:
]# ls 00 0f 15 28 2c 39 3c 43 4c 54 5e 66 7b 7f 8b 8e 92 97 a6 ab b4 bb c7 f5 fc 02 14 1e 29 2e 3a 40 47 4d 59 5f 71 7c 85 8c 8f 93 a2 a9 ad b7 c6 e2 f9 fd ]# pwd /var/cache/nginx/proxy
以上是关于nginx负载均衡LAMP及基于memcached实现php会话保存的主要内容,如果未能解决你的问题,请参考以下文章
KVM虚拟化搭建nginx负载均衡 和lamp 架构(三 nginx负载均衡)
Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第一部分(LAMP)
nginx+keepalived+tomcat+memcache负载均衡搭建小集群