OpenResty实现LNMP的缓存前移(到达nginx前端层面),
一、什么是OpenResty
- OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。通过汇聚各种设计精良的 Nginx 模块,从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
- OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
二、缓存前移的实现
[root@server1 ~]# nginx -s stop
- 官网下载openresty源码包,解压并源码编译
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz
[root@server1 ~]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure --prefix=/usr/local/lnmp/openresty # 编译预环境,与nginx的源码编译一样,唯一改了一下安装路径
--with-http_ssl_module
--with-http_stub_status_module
--user=nginx
--group=nginx
--with-threads
--with-file-aio
[root@server1 openresty-1.13.6.1]# gmake && gmake install ##注意此处是gmake && gmake install
- 将上一篇博文最后用于测试的两个文件复制到指定目录下,用于后续的测试
[root@server1 local]# cd openresty/nginx/ html/
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php
- 修改openresty的配置文件
[root@server1 nginx]# cd /usr/local/openresty/nginxconf/
[root@server1 conf]# vim nginx.conf
修改部分:
17 http {
18 upstream memcache{
19 server localhost:11211;
20 keepalive 512;
21 }
22 include mime.types;
23 default_type application/octet-stream;
新添加:
54 location /memc {
55 internal; # 只允许内部
56 memc_connect_timeout 100ms;
57 memc_send_timeout 100ms;
58 memc_read_timeout 100ms;
59 set $memc_key $query_string; # 键值对的形式存储
60 set $memc_exptime 300;
61 memc_pass memcache;
62 }
修改:
78 location ~ \.php$ {
79 set $key $uri$args;
80 srcache_fetch GET /memc $key;
81 srcache_store PUT /memc $key;
82 root html;
83 fastcgi_pass 127.0.0.1:9000;
84 fastcgi_index index.php;
85 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
86 include fastcgi.conf;
87 }
- 检测openresty配置文件是否有语法错误,并打开openstry
[root@server1 nginx]# cd /usr/local/openresty/nginx/sbin/
[root@server1 sbin]# ./nginx -t
[root@server1 sbin]# ./nginx
[root@server1 sbin]# netstat -tnlp ##端口为11211
- 测试:浏览器输入:172.25.79.1
- 压力测试
(1)访问 index.php
[root@server1 ~]# ab -c 10 -n 5000 http://172.25.79.1/index.php
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20190220105728683.png)
(2)访问 example.php
```javascript
[root@server1 ~]# ab -c 10 -n 5000 http://172.25.79.1/example.php
- 可以发现,相比上一篇博文中的一层缓存,再加一层缓存后,速度都大大提高,且出错率都为0
评论暂时关闭