Nginx 採用一個 master 進程管理多個 worker 進程(master-worker)模式,基本的事件處理都在 woker 中,master 負責一些全局初始化,以及對 worker 的管理。在OpenResty中,每個 woker 使用一個 LuaVM,當請求被分配到 woker 時,將在這個 LuaVM 裏創建一個 coroutine(協程)。協程之間數據隔離,每個協程具有獨立的全局變量_G。OpenResty致力於將服務器應用完全運行與nginx中,充分利用nginx事件模型進行非阻塞I/O通信。其對MySQL、redis、Memcached的I\O通信操作也是非阻塞的,可以輕鬆應對10K以上的超高連接併發。
OpenResty 是 Nginx 與 Lua 的結合;
下載 http://openresty.org/cn/download.html
安裝前的準備
必須將這些庫 perl 5.6.1+, libpcre, libssl安裝在您的電腦之中
yum install pcre-devel openssl-devel gcc curl -y
安裝
tar -zxvf openresty-1.15.8.2.tar.gz
cd openresty-1.15.8.2
./configure
make
make install
默認程序會被安裝到/usr/local/openresty目錄。
配置
修改nginx.conf配置文件
cd /usr/local/openresty/nginx/conf
vim nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
';
}
}
}
添加環境變量
echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile
然後啓動openresty,啓動命令和nginx一致。
nginx -c /usr/local/openresty/nginx/conf/nginx.conf
啓動後查看一下服務
ps -ef | grep nginx
訪問 Web 服務
第二種lua配置方案
添加lua.conf配置文件
server {
listen 8080;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
';
}
}
修改nginx.conf配置文件
cd /usr/local/openresty/nginx/conf
vim nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
#lua模塊路徑,多個之間”;”分隔,其中”;;”表示默認搜索路徑,默認到/usr/servers/nginx下找
lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #lua模塊
lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; #c模塊
include lua.conf; #lua.conf和nginx.conf在同一目錄下
}
添加環境變量
echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile
然後啓動openresty,啓動命令和nginx一致。
nginx -c /usr/local/openresty/nginx/conf/nginx.conf
啓動後查看一下服務
ps -ef | grep nginx
訪問 Web 服務
配置lua代碼文件
我們把lua代碼放在nginx配置中會隨着lua的代碼的增加導致配置文件太長不好維護,因此我們應該把lua代碼移到外部文件中存儲。
在conf文件夾下創建lua文件夾,專門用來存放lua文件
mkdir /usr/local/openresty/nginx/conf/lua
創建test.lua文件
cd /usr/local/openresty/nginx/conf/lua
vim test.lua
ngx.say("test lua");
修改conf/lua.conf文件
vim /usr/local/openresty/nginx/conf/lua.conf
server {
listen 8080;
location / {
default_type text/html;
lua_code_cache off; #關閉lua代碼緩存,調試時關閉,正式環境開啓
content_by_lua_file conf/lua/test.lua; #相對於nginx安裝目錄
}
}
關閉緩存後會看到如下報警(忽略不管)
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/lua.conf:5
重啓 Web 服務
nginx -s reload
.測試性能
安裝ab命令
yum -y install httpd-tools
壓力測試
- -c:每次併發數爲10個
- -n:共發送50000個請求
ab -c10 -n50000 http://localhost:8080/
測試報詳解
Server Software: openresty/1.15.8.2 #服務器軟件
Server Hostname: localhost #域名
Server Port: 8080 #端口
Document Path: / #文件路徑
Document Length: 20 bytes #頁面字節數
Concurrency Level: 10 #請求併發數
Time taken for tests: 3.392 seconds #總訪問時間
Complete requests: 50000 #請求成功數
Failed requests: 0 #請求失敗數
Write errors: 0
Total transferred: 8400000 bytes #請求總數據大小(包括header頭信息)
HTML transferred: 1000000 bytes #html頁面實際總字節數
Requests per second: 14738.52 [#/sec] (mean) #每秒多少請求,這個是非常重要的參數數值,服務器的吞吐量
Time per request: 0.678 [ms] (mean) #用戶平均請求等待時間
Time per request: 0.068 [ms] (mean, across all concurrent requests) # 服務器平均處理時間,也就是服務器吞吐量的倒數
Transfer rate: 2418.04 [Kbytes/sec] received #每秒獲取的數據長度
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 1 0.2 1 9
Waiting: 0 0 0.2 0 9
Total: 0 1 0.2 1 9
Percentage of the requests served within a certain time (ms)
50% 1 #50%用戶請求在1ms內返回
66% 1 #66%用戶請求在1ms內返回
75% 1 #75%用戶請求在1ms內返回
80% 1 #80%用戶請求在1ms內返回
90% 1 #90%用戶請求在1ms內返回
95% 1 #95%用戶請求在1ms內返回
98% 1 #98%用戶請求在1ms內返回
99% 1 #99%用戶請求在1ms內返回
100% 9 (longest request)