一、高速缓存memc
传统缓存策略由php控制生命周期,高效缓存策略由memc+srcache在nginx部分缓存
Openresty里面有nginx模块,也有缓存模块,所以要用高速缓存,需要使Openresty里面的nginx运行
lftp 172.25.254.250:/pub/docs/lamp> get openresty-1.19.9.1.tar.gz
[root@server1 ~]# tar zxf openresty-1.19.9.1.tar.gz ###下载openresty并解压
[root@server1 ~]# cd openresty-1.19.9.1
[root@server1 openresty-1.19.9.1]# ls ###有configure文件,可编译make
[root@server1 openresty-1.19.9.1]# ./configure --with-http_ssl_module ###加参数
[root@server1 openresty-1.19.9.1]# make && make install ###安装
[root@server1 openresty-1.19.9.1]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf
修改用户,并发数改为auto,openfile改为65535
添加memcache参数
开启memcache,查看端口是否为11211
[root@server1 conf]# systemctl start memcached.service
[root@server1 conf]# netstat -antlp
继续编辑配置文件
开启php,查看端口是否为9000
[root@server1 conf]# systemctl start php-fpm
[root@server1 conf]# netstat -antlp
vim nginx.conf----->取消php注释,修改并添加参数
[root@server1 html]# cp /usr/local/nginx/html/example.php .
复制/usr/local/nginx/html下nginx的默认发布目录文件到/usr/local/openresty/nginx/html的默认发布目录中
测试:开启/usr/local/nginx,真机测试:ab -c10 -n5000 http://172.25.8.1/example.php
并发数961
停掉local下的nginx,开启/usr/local/openresty/nginx的nginx
/usr/local/nginx -s stop
/usr/local/openresty/nginx -t
/usr/local/openresty/nginx -s reload
真机测试:ab -c10 -n5000 http://172.25.8.1/example.php
并发数7000
二、Tomcat结合memc
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s stop ###server1的nginx停掉
Server2:
[root@server2 ~]# lftp 172.25.254.250
lftp 172.25.254.250:~> cd /pub/docs/lamp/
lftp 172.25.254.250:/pub/docs/lamp> get apache-tomcat-7.0.37.tar.gz ###下载tomcat
lftp 172.25.254.250:/pub/docs/lamp> mirror jar ###下载jar
lftp 172.25.254.250:/pub/docs/lamp> get jdk-8u121-linux-x64.rpm ###下载jdk
[root@server2 ~]# rpm -ivh jdk-8u121-linux-x64.rpm ##解压
[root@server2 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server2 ~]# cd /usr/local/
[root@server2 local]# ln -s apache-tomcat-7.0.37/ tomcat ###建立软链接
[root@server2 ~]# /usr/local/tomcat/bin/startup.sh ###开启tomcat
Server1中:
[root@server1 conf]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf ###编辑配置文件
访问.jsp,2的8080端口
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload ###重新加载
浏览器访问
172.25.8.2:8080
172.25.8.1/index.jsp
[root@server1 conf]# vim nginx.conf ###编辑配置文件
域名访问
[root@server2 ~]# cd /usr/local/tomcat/webapps/ROOT ###tomcat的默认发布目录
lftp 172.25.254.250:/pub/docs/lamp> get test.jsp ###下载test测试页
浏览器访问:www.westos.org/test.jsp
此方式不能实现高可用,当server2down掉,就不能用了
解决方法:再搭建一台server3,用来做负载均衡和高可用
[root@server2 ~]# scp apache-tomcat-7.0.37.tar.gz jdk-8u121-linux-x64.rpm server3: ####复制文件到server3
[root@server3 ~]# rpm -ivh jdk-8u121-linux-x64.rpm
[root@server3 ~]# tar zxf apache-tomcat-7.0.37.tar.gz (-C /usr/local/) ###解压文件
[root@server3 ~]# mv apache-tomcat-7.0.37 /usr/local/ ###保证文件位置和server2同路径
[root@server3 local]# ln -s apache-tomcat-7.0.37/ tomcat ###建立软链接
[root@server3 tomcat]# /usr/local/tomcat/bin/startup.sh ###开启tomcat
[root@server3 tomcat]# cd /usr/local/tomcat/webapps/ROOT/
lftp 172.25.254.250:/pub/docs/lamp> get test.jsp ###默认发布目录下载test
Server1中:
[root@server1 conf]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
组名为tomcat,在2和3添加端口8080,打开哈系算法
域名访问组的名称改为tomcat
测试:第一次访问是2
[root@server2 ROOT]# /usr/local/tomcat/bin/shutdown.sh
2down掉后访问3
[root@server2 ROOT]# /usr/local/tomcat/bin/startup.sh
2再开启会继续访问2,但之前访问2的数据丢失
三、设置sticky
重新编译,只make,不用make install
复制新的nginx文件到原文件目录下,覆盖
配置文件中删除sticky注释
nginx -t
nginx
浏览器上访问测试页,粘滞开启
四、交叉存储
在server2和server3中分别下载memcached并启动
systemctl start memcached
保证server2和server3上都有jar包
在server2上:
关闭tomcat
编辑tomcat配置文件
[root@server2 tomcat]# cd conf/
[root@server2 conf]# vim context.xml
Server2中:2会访问3的memc(交叉存储)
Failovernodes:如果访问失败,则去访问n1(自己)
复制jar包到/usr/local/tomcat/lib
cp ~/jar/* .
删除tc6,避免冲突
开启tomcat: bin/startup.sh
查看日志:
显示交叉存储信息
在server3上:
编辑tomcat的配置文件
添加交叉存储策略
Server2中:3会访问2的memc(交叉存储)
Failovernodes:如果访问失败,则去访问n2(自己)
复制jar包到/usr/local/tomcat/lib
删除tc6,避免冲突
开启tomcat: bin/startup.sh
查看日志:
显示交叉存储信息
测试:在2、3的memcached和tomcat都开启的时候,2对应的是n2(3)
若关闭3的tomcat:bin/shutdown.sh,测试页的历史输入还在,访问ip变为3,对应变为n1
若关闭3的memcached:systemctl stop memcached:历史输入还在,ip还是2,变为n2(2的n1挂掉了,访问自己)
若3的memcached和tomcat都down掉(server3poweroff)2的历史输入不存在了,因为2访问的3的memc,2的存储变为n2
五、nginx限流
vim nginx.conf ####编辑配置文件
limit_conn addr 1; ###并发数为1
limit_req zone=one burst=5 nodelay; ###排队大小为5,如果有10个请求,除了正在处理的会有5个排队,剩下4个不会被处理,报错
limit_rate 50k; ###限速50K
limit_conn_zone $binary_remote_addr zone=addr:10m; ###开辟一个临时的addr空间,大小为10m
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ###1秒处理一个
nginx -t
nginx -s reload
[root@server1 conf]# cd /usr/local/nginx/html
[root@server1 html]# mkdir download/
lftp 172.25.254.250:/pub/docs> get vim.jpg
真机下载测试:
[kiosk@foundation8 Desktop]$ ab -c1 -n5 http://172.25.8.1/download/vim.jpg
查看日志:cd /usr/local/nginx/logs
[root@server1 logs]# cat access.log
六、nginx限流
有效期365天
编写脚本,添加执行权限
编译安装
执行脚本,生成往期日志
数据可视化
中文乱码
只允许本机访问
地址转换
请求返回报错500
转换到www.westos.org
证书为cert.pem
生成证书
移动证书到conf下
nginx -t
nginx -s reload