lnmp架构(四)——利用OpenResty实现nginx缓存前移(将memcache移到nginx前端层面)

一、什么是openresty?

OpenResty(又称: ngx_ openresty) 是一个基于NGINX的可伸缩的Web平台,由中国人章亦春发起,提供了很多高质歌的第三方模块。

OpenResty是一个基于 NginxLua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty的目标是让web服务直接运行在Nginx服务内部,利用Ngnix的非阻塞IO模型,对HTTP客户端请求和后端DB进行一致的高性能响应

OpenResty访问Mysq|或Redis等,而不需要通过第三方语言( PHP、Python、Ruby )等来访问数据库再返回,这大大提高了应用的性能。

参考openresty中文官网http://openresty.org/cn

1、运行原理

在这里插入图片描述
Nginx 采用的是 master-worker 模型,一个 master 进程管理多个 worker 进程

  • woker负责基本事件的处理
  • master 负责一些全局初始化,以及对 worker 的管理

在OpenResty中,每个 woker 使用一个 LuaVM,当请求被分配到 woker 时,将在这个 LuaVM 里创建一个 coroutine(协程)协程之间数据隔离,每个协程具有独立的全局变量_G

二、利用OpenResty给nginx添加memcache(缓存前移的实现)

在上一篇博客lnmp架构(三)中,我们给php添加了memcache模块,现在我们利用OpenResty给nginx添加memcache模块,这样不用等待第三方返回,大大提高了应用的性能。

实验环境

主机(版本:ip) 功用
虚拟机server1(rhel7.3:172.25.16.1) lnmp环境主机
真机(rhel7.3:172.25.16.250) 测试机

我们先关闭之前配置好的nginx,因为openresty是一个类似于nginx的服务,调用都是80端口。
在这里插入图片描述
step1 解压并源码编译openresty源码包:

tar zxf openresty-1.13.6.1.tar.gz 
cd openresty-1.13.6.1
./configure --prefix=/usr/local/openresty
gmake && gmake install

step2 修改openresty的配置文件:

cd /usr/local/openresty/
cd nginx/
cd conf/
nginx -s stop	#关闭之前的nginx服务
vim nginx.conf

  1 
  2 user  nginx nginx;

 17 http {
 18     include       mime.types;
 19     default_type  application/octet-stream;
 20         upstream memcache {
 21                 server localhost:11211;
 22                 keepalive 512;
 23 }

 69         location /memc {
 70                 internal;       #只接收内部访问,不接受外部http访问。比较安全
 71                 memc_connect_timeout 100ms;
 72                 memc_send_timeout 100ms;        #后端服务器数据回传时间
 73                 memc_read_timeout 100ms;        #连接成功后,后端服务器响应时间
 74                 set $memc_key $query_string;
 75                 set $memc_exptime 300;
 76                 memc_pass memcache;
 77         }
 78 
 79         location ~ \.php$ {
 #http的GET方法表示get、PUT方法表示set
 80             set $key $uri$args;
 81             srcache_fetch GET /memc $key;		#请求php页面时,会先去memcache中找。如果没有,正常访问;
 82             srcache_store PUT /memc $key;		#访问结束后将结果存到memcache中,下次访问时直接从缓存中取
 83             root           html;
 84             fastcgi_pass   127.0.0.1:9000;
 85             fastcgi_index  index.php;
 86         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 87             include        fastcgi.conf;
 88         }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
step3 检查没有语法错误之后开启服务,查看端口号

[root@server1 html] /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 html] /usr/local/openresty/nginx/sbin/nginx 
[root@server1 html] netstat -tlnp

在这里插入图片描述
step4 将测试文件复制到openresty的默认发布目录下:

cp /usr/local/lnmp/nginx/html/index.php /usr/local/openresty/nginx/html/
cp /usr/local/lnmp/nginx/html/example.php /usr/local/openresty/nginx/html/

step5 启动openstry:

/usr/local/openresty/nginx/sbin/nginx 

在这里插入图片描述
step6 测试:

ab -c 10 -n 5000 http://172.25.254.1/index.php
ab -c 10 -n 5000 http://172.25.254.1/example.php

在这里插入图片描述
在这里插入图片描述
通过测试可以发现:相比上一篇博文中的一层缓存,再加一层缓存后,速度大大提高,且出错率为0

现在的web服务器是 nginx+memcache等于openresty

openResty是nginx的plus版,很多nginx做不到的事情,可以用openResty来做

curl -I 172.25.16.1

在这里插入图片描述
此时服务软件是openresty

发布了184 篇原创文章 · 获赞 7 · 访问量 3623

猜你喜欢

转载自blog.csdn.net/weixin_43936969/article/details/104706666