一:簡介
OpenResty(又稱:ngx_openresty) 是一個基于 NGINX 的可伸縮的 Web 平台,由中國人章亦春發起,提供了很多高品質的第三方子產品。
OpenResty 是一個強大的 Web 應用伺服器,Web 開發人員可以使用 Lua 腳本語言調動 Nginx 支援的各種 C 以及 Lua 子產品,更主要的是在性能方面,OpenResty可以 快速構造出足以勝任 10K 以上并發連接配接響應的超高性能 Web 應用系統。
360,UPYUN,阿裡雲,新浪,騰訊網,去哪兒網,酷狗音樂等都是 OpenResty 的深度使用者。
二:安裝依賴包
#yum install readline-devel pcre-devel openssl-devel gcc
三:安裝openresty
3.1下載下傳openresty源碼
#wget https://openresty.org/download/openresty-1.9.7.4.tar.gz
3.2解壓安裝包
#tar xzvf openresty-1.9.7.4.tar.gz
3.3配置編譯選項,可以根據你的實際情況增加、減少相應的子產品
#cd openresty-1.9.7.4/
#./configure --prefix=/opt/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module
3.4編譯安裝
# make
#make install
四:使用
4.1修改配置檔案如下
# cat /opt/openresty/nginx/conf/nginx.conf
worker_processes 1;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
server {
listen 8003;
location / {
content_by_lua 'ngx.say("hello world.")';
}
}
}
4.2啟動nginx
#/opt/openresty/nginx/sbin/nginx
4.3使用nginx
# curl http://127.0.0.1:8003/
hello world.
五:性能測試
5.1安裝壓力測試工具
#yum install httpd-tools
5.2測試
# ab -c10 -n50000 http://localhost:8003/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests
Server Software: openresty/1.9.7.4
Server Hostname: localhost
Server Port: 8003
Document Path: /
Document Length: 13 bytes
Concurrency Level: 10
Time taken for tests: 7.857 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 8050000 bytes
HTML transferred: 650000 bytes
Requests per second: 6363.75 [#/sec] (mean)
Time per request: 1.571 [ms] (mean)
Time per request: 0.157 [ms] (mean, across all concurrent requests)
Transfer rate: 1000.55 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 1
Processing: 1 1 0.2 1 4
Waiting: 1 1 0.1 1 3
Total: 1 2 0.1 2 4
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 2
95% 2
98% 2
99% 2
100% 4 (longest request)
六:nginx服務相關指令
6.1啟動
#/opt/openresty/nginx/sbin/nginx
6.2停止
#/opt/openresty/nginx/sbin/nginx -s stop
6.3重新開機
#/opt/openresty/nginx/sbin/nginx -s reload
6.4檢驗nginx配置是否正确
# /opt/openresty/nginx/sbin/nginx -t