LNMP架構---OpenResty實現緩存前移(到達nginx層面)

LNMP架構—OpenResty實現緩存前移(到達nginx層面)

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訪問Mysq|或Redis等,而不需要通過第三方語言( PHP、Python、Ruby )等來訪問數據庫再返回,這大大提高了應用的性能。

參考openresty中文官網http://openresty.org/cn

2.緩存前移的實現

step1 解壓並源碼編譯openresty源碼包:

tar zxf openresty-1.13.6.1.tar.gz 
cd openresty-1.13.6.1
./configure --prefix=/usr/local/openresty
gmake && gmake install

step2 修改openresty的配置文件:

cd /usr/local/openresty/
cd nginx/
cd conf/
nginx -s stop	#關閉之前的nginx服務
vim nginx.conf

  1 
  2 user  nginx nginx;

 17 http {
 18     include       mime.types;
 19     default_type  application/octet-stream;
 20         upstream memcache {
 21                 server localhost:11211;
 22                 keepalive 512;
 23 }

 69         location /memc {
 70                 internal;       #只接收內部訪問,不接受外部http訪問。比較安全
 71                 memc_connect_timeout 100ms;
 72                 memc_send_timeout 100ms;        #後端服務器數據回傳時間
 73                 memc_read_timeout 100ms;        #連接成功後,後端服務器響應時間
 74                 set $memc_key $query_string;
 75                 set $memc_exptime 300;
 76                 memc_pass memcache;
 77         }
 78 
 79         location ~ \.php$ {
 #http的GET方法表示get、PUT方法表示set
 80             set $key $uri$args;
 81             srcache_fetch GET /memc $key;		#請求php頁面時,會先去memcache中找。如果沒有,正常訪問;
 82             srcache_store PUT /memc $key;		#訪問結束後將結果存到memcache中,下次訪問時直接從緩存中取
 83             root           html;
 84             fastcgi_pass   127.0.0.1:9000;
 85             fastcgi_index  index.php;
 86         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 87             include        fastcgi.conf;
 88         }

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述
step3 將測試文件複製到openresty的默認發佈目錄下:

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/

step4 啓動openstry:

/usr/local/openresty/nginx/sbin/nginx 

在這裏插入圖片描述
step5 測試:

ab -c 10 -n 5000 http://172.25.254.1/index.php
ab -c 10 -n 5000 http://172.25.254.1/example.php

在這裏插入圖片描述
在這裏插入圖片描述
通過測試可以發現:相比上一篇博文中的一層緩存,再加一層緩存後,速度大大提高,且出錯率爲0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章