1.什么是OpenResty?
OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web平台,由中国人章亦春发起,提供了很多高质量的第三方模块。
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统。
360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。
OpenResty 的目标是让你的 Web 服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL,PostgreSQL,~Memcaches 以及 ~Redis 等都进行一致的高性能响应。
所以对于一些高性能的服务来说,可以直接使用 OpenResty 访问 Mysql或Redis等,而不需要通过第三方语言(PHP、Python、Ruby)等来访问数据库再返回,这大大提高了应用的性能。
参考openresty中文官网 http://openresty.org/cn/
原nginx不支持memcache
1)给nginx添加memc和sr cache模块,让nginx直接访问memcache来提高速度
先关闭原来的nginx,因为下面安装的openresty-1.13.6.1.tar.gz也是nginx
nginx -s stop
源码编译安装openresty-1.13.6.1.tar.gz(不需要加参数,使用默认)
./configure --prefix=/usr/local/openresty
gmake && gmake install
拷贝之前example.php和index.php到默认发布目录准备测试
/usr/local/openresty/nginx/html
[root@server5 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server5 html]# cp /usr/local/lnmp/nginx/html/example.php .
修改nginx配置文件:vim /usr/local/openresty/nginx/conf/nginx.conf
添加:
upstream memcache {
server localhost:11211;
keepalive 512;
}
location /memc {
internal; # 只接收内部访问,不接受外部http访问。比较安全
memc_connect_timeout 100ms;
memc_send_timeout 100ms; ##后端服务器数据回传时间
memc_read_timeout 100ms; ##连接成功后,后端服务器响应时间
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
##http的GET方法表示get、PUT方法表示set
srcache_fetch GET /memc $key;(这两个配置的作用是:请求php页面时,先会去memcache中找,如果没有,正常访问;
srcache_store PUT /memc $key; 访问结束后将结果存到memcache,下次访问时直接从缓存中)
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
再次进行压测,速度会比之前只加了php的缓存更快
###重点说明一下对index.php页面的访问
访问速度有了明显的差别 因为php是没有缓存的