一、什麼是openresty?
OpenResty是一個基於 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用於方便地搭建能夠處理超高併發、擴展性極高的動態 Web 應用、Web 服務和動態網關。 通過匯聚各種設計精良的 Nginx 模塊(主要由 OpenResty 團隊自主開發),從而將 Nginx 有效地變成一個強大的通用 Web 應用平臺。這樣,Web 開發人員和系統工程師可以使用 Lua 腳本語言調動 Nginx 支持的各種 C 以及 Lua 模塊,快速構造出足以勝任 10K 乃至 1000K 以上單機併發連接的高性能 Web 應用系統。 OpenResty的目標是讓web服務直接運行在Nginx服務內部,利用Ngnix的非阻塞IO模型,對HTTP客戶端請求和後端DB進行一致的高性能響應。
1、運行原理
Nginx 採用的是 master-worker
模型,一個 master 進程管理多個 worker 進程
- woker負責基本事件的處理
- master 負責一些全局初始化,以及對 worker 的管理
在OpenResty中,每個 woker 使用一個 LuaVM
,當請求被分配到 woker 時,將在這個 LuaVM 裏創建一個 coroutine(協程
)協程之間數據隔離,每個協程具有獨立的全局變量_G
二、利用OpenResty給nginx添加memcache
在上一篇博客lnmp架構(四)中,我們給php添加了memcache模塊
,現在我們利用OpenResty給nginx添加memcache模塊
,這樣不用等待第三方返回,大大提高了應用的性能。
實驗環境
主機(版本:ip) | 功用 |
---|---|
虛擬機server1(rhel6.5:172.25.2.1) | lnmp環境主機 |
真機(rhel7.3:172.25.2.250) | 測試機 |
1、我們先關閉之前配置好的nginx,因爲openresty是一個類似於nginx的服務,調用都是80端口。
2、從真機下載openresty的包到虛擬機,並解壓
tar zxf openresty-1.13.6.1.tar.gz
3、進入解壓目錄cd openresty-1.13.6.1
,直接編譯並安裝。
cd openresty-1.13.6.1
./configure --prefix=/usr/local/openresty
gmake && gmake install
4、拷貝之前example.php
和index.php
到默認發佈目錄準備測試
[root@server1 openresty-1.13.6.1]# cd /usr/local/openresty/nginx/html
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
5、編輯openresty的nginx配置文件/usr/local/openresty/nginx/conf/nginx.conf
vim /usr/local/openresty/nginx/conf/nginx.conf
17 http {
18 upstream memcache {
19 server localhost:11211;
20 keepalive 512;
21 }
22
#upstream屬於handler,只是他不產生自己的內容,而是通過請求後端服務器得到內容,所以才稱爲upstream(上游)。請求並取得響應內容的整個過程已經被封裝到nginx內部,所以upstream模塊只需要開發若干回調函數,完成構造請求和解析響應等具體的工作。
# nginx將memcache緩存前移,客戶端請求到來,先查看memcache緩存
23 include mime.types;
24 default_type application/octet-stream;
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$ {
80 set $key $uri$args; # http的GET方法表示get、PUT方法表示set
81 srcache_fetch GET /memc $key;
82 srcache_store PUT /memc $key;
# 這兩個配置的作用是:請求php頁面時,先會去memcache中找,如果沒有,正常訪問;訪問結束後將結果存到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 }
6、檢查沒有語法錯誤之後開啓服務,查看端口號
[root@server1 html]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 html]# /usr/local/openresty/nginx/sbin/nginx
[root@server1 html]# netstat -tlnp
7、 複製之前更改過的/usr/local/lnmp/nginx/html/memcache.php
到/usr/local/openresty/nginx/html
目錄下
cp /usr/local/lnmp/nginx/html/memcache.php /usr/local/openresty/nginx/html
8、訪問頁面:http://172.25.2.1/memcache.php
輸入用戶名密碼,查看memcache命中率,
9、在真機,壓力測試,模擬5000請求量
ab -c 10 -n 5000 http://172.25.2.1/index.php
發現訪問index.php沒有失敗,而且訪問時間都比給php添加memcache短
ab -c 10 -n 5000 http://172.25.2.1/example.php
訪問速度有了明顯的差別 ,因爲php是沒有緩存的