文章目录

  • OpenResty简介
  • 模拟缓存前移
  • 分析
  • 压力测试


OpenResty简介

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

OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统

模拟缓存前移

  • 1.关闭之前lnmp架构下开启的nginx服务
nginx -s stop
  • 2.官网下载OpenResty的原码包解压并编译
    注意:OpenResty的编译安装使用的是gmake
tar zxf openresty-1.13.6.1.tar.gz
 cd openresty-1.13.6.1
 ./configure
 gmake && gmake install
  • 3.将memcache用于测试的俩个php文件复制到OpenResty的Nginx的默认发布目录下
cp /usr/local/lnmp/nginx/html/example.php /usr/local/openresty/nginx/html
cp /usr/local/lnmp/nginx/html/memcache.php /usr/local/openresty/nginx/html
  • 4.编辑openresty的nginx配置文件
vim /usr/local/openresty/nginx/conf/nginx.conf

写入:

http {
        upstream memcache {   ##所有的请求,通过转发给memcache缓存服务
           server localhost:11211;   ##memcache服务在本机的11211端口
           keepalive 512;
       }
 location /memc { ##设置memcache的访问及其存储 
         internal; ##只允许内部 
         memc_connect_timeout 100ms; #定义连接发送及其超时时间 
         memc_send_timeout 100ms;
         memc_read_timeout 100ms;
         set $memc_key $query_string; #memcache以键值对的形式存储 
         set $memc_exptime 300;
         memc_pass memcache;
         }
     location ~ \.php$ {
           set $key $uri$args;
           srcache_fetch GET /memc $key;
           srcache_store PUT /memc $key;
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           include        fastcgi.conf;         ##定义nginx与php连接的时识别的文件
       }

配置文件相关参数详解如下:

  • upstream模块接口

从本质上说,upstream属于handler,只是他不产生自己的内容,而是通过请求后端服务器得到内容,所以才称为upstream(上游)。请求并取得响应内容的整个过程已经被封装到nginx内部,所以upstream模块只需要开发若干回调函数,完成构造请求和解析响应等具体的工作。

所有请求都是通过location来操作memcache,memc-nginx-module存取memcache是基于http method语义的

使用http的GET方法表示get,PUT方法表示set,设置memcache的缓存方式为internal只接受内部访问

memc_key表示以什么作为key,文件中使用的是query_string来作为key,$memc_exptime表示缓存失效时间,单位为秒

  • 5.检查配置文件是否有错误并且开启服务
[root@LNMPserver1 html]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
  • 6.开启服务,查看端口
[root@LNMPserver1 html]# /usr/local/openresty/nginx/sbin/nginx 
[root@LNMPserver1 html]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      6843/memcached      
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      15320/nginx         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      5089/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      962/master          
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      6748/php-fpm        
tcp        0      0 :::11211                    :::*                        LISTEN      6843/memcached      
tcp        0      0 :::22                       :::*                        LISTEN      5089/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      962/master          
tcp        0      0 :::3306                     :::*                        LISTEN      31925/mysqld
  • 7.浏览器查看

    至此,结合OpenResty的缓存前移已经做好了,我们来总结分析一下。

分析

我们这里主要干了一件事,就是在OpenResty的nginx配置文件中,指定了upstream memcache,这也是我们使用OpenResty的原因,因为它集成了memcache等模块,我们可以清晰的从其配置文件中理解,所有的请求,会经由本地memcache的11211端口,而且

location ~ \.php$ {
           set $key $uri$args;
           srcache_fetch GET /memc $key;
           srcache_store PUT /memc $key;

这里指定了所有访问.php结尾的页面,会从 /memc 中取,没有的话就存进去。所以,我们相当于让memcache前移到Nginx接受请求的位置。

压力测试

我们再次在真实主机上测试:

ab -c 10 -n 2000 http://172.25.66.1/index.php
ab -c 10 -n 2000 http://172.25.66.1/example.php

可以发现,两个页面的响应时间和正确率都有质的提高。而有两级的memcache的example.php页面速度更快了。

openresty lua各个阶段 openresty架构_nginx


openresty lua各个阶段 openresty架构_openresty lua各个阶段_02