一 、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 )等来访问数据库再返回,这大大提高了应用的性能。
目的: 普通的nginx 与 加了mencache的nginx(即OpenResty)
测试: nginx 和 openresty 的不同的web服务器,找其区别!!!
二 、OpenResty的实现
步骤一:解压openresty,并编译
tar zxf openresty-1.13.6.1.tar.gz
cd openresty-1.13.6.1
./configure --prefix=/usr/local/openresty
gmake && gmake install
步骤二:关闭上个项目的nginx,并配置openresty的nginx文件
nginx -s stop
cd /usr/local/openresty/nginx/conf/
vim nginx.conf
user nginx nginx;
http {
include mime.types;
default_type application/octet-stream;
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页面时,先回取memecache中找,如果没有找到,正常访问
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;
}
拷贝文件
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/
cd /usr/local/openresty/nginx/sbin/
./nginx
测试
ab -c 10 -n 5000 http://172.25.42.1/index.php
Concurrency Level: 10
Time taken for tests: 13.763 seconds
Complete requests: 5000
ab -c 10 -n 5000 http://172.25.42.1/example.php
Concurrency Level: 10
Time taken for tests: 1.907 seconds
Complete requests: 5000