| 
       
	Linux运维之LNMP架构(openresty) 
	什么是openresty 
	原生nginx不支持memcache 
	所以在做nginx的memcache模块时需要使用openresty 
	为nginx添加memc和sr cache模块,让nginx直接访问memcache来提高速度 
	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的部署 
	在安装编译过原生nginx的基础上 
	关闭ngixn 
	nginx -s stop 
	openresty解压编译安装 
	tar zxf openresty-1.13.6.1.tar.gz  
	cd openresty-1.13.6.1 
	./configure --prefix=/usr/local/openresty 
	gmake && gmake install 
	拷贝之前example.php和index.php到默认发布目录准备测试 
	cd /usr/local/openresty/nginx/html 
	cp /usr/local/lnmp/nginx/html/index.php . 
	cp /usr/local/lnmp/nginx/html/example.php . 
	修改openresty的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; 
	        } 
	编写配置完毕检测语法并开启nginx 
	/usr/local/openresty/nginx/sbin/nginx -t 
	/usr/local/openresty/nginx/sbin/nginx 
	再次对网页进行压力测试: 
	ab -c 10 -n 5000 http://172.25.11.4/index.php 
	ab -c 10 -n 5000 http://172.25.11.4/example.php 
	与之前只设定了php的memcache的网页进行对比 
	发现速度会比之前只加了php的缓存更快 
	重点说明一下对index.php页面的访问,访问速度有了明显的差别 因为php是没有缓存的 
	END 
 | 
    
