一.簡介
OpenResty® 是一個基於 Nginx 與 Lua 的高性能 Web 平台,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用於方便地搭建能夠處理超高並發、擴展性極高的動態 Web 應用、Web 服務和動態網關。
OpenResty® 通過匯聚各種設計精良的 Nginx 模塊(主要由 OpenResty 團隊自主開發),從而將 Nginx 有效地變成一個強大的通用 Web 應用平台。這樣,Web 開發人員和系統工程師可以使用 Lua 腳本語言調動 Nginx 支持的各種 C 以及 Lua 模塊,快速構造出足以勝任 10K 乃至 1000K 以上單機並發連接的高性能 Web 應用系統。
OpenResty® 的目標是讓你的Web服務直接跑在 Nginx 服務內部,充分利用 Nginx 的非阻塞 I/O 模型,不僅僅對 HTTP 客戶端請求,甚至於對遠程后端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高性能響應。來自OpenResty®官網
總結和拓展:
- OpenResty 是 Nginx 與 Lua 的結合;
- OpenResty 是多進程模式,會有一個 master 進程和多個 worker 進程。Master 進程管理 worker 進程,向各 worker 進程發送信號,監控 work 進程狀態;
- OpenResty 是異步非阻塞 ;怎樣理解阻塞非阻塞與同步異步的區別?知乎
- 子查詢:OpenResty 中有三種方式發起子請求:capture、exec、redirect;
- OpenResty 緩存機制。
Nginx+Lua架構思維導圖:
二.關閉SELinux
先臨時關閉,然后永久關閉
setenforce 0 # 臨時關閉
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config # 永久關閉
三.防火牆開啟80端口
先臨時開啟80端口,然后再永久開啟
iptables -I INPUT -p tcp --dport 80 -j ACCEPT # 臨時生效
/etc/init.d/iptables save # 保存到配置文件,永久生效
/etc/init.d/iptables status # 查看iptables當前狀態
四.yum安裝
對於一些常見的 Linux 發行版本,OpenResty® 提供 官方預編譯包。確保你首先用這種方式來安裝。
你可以在你的 CentOS 系統中添加 openresty 倉庫,這樣就可以便於未來安裝或更新我們的軟件包(通過 yum update 命令)。運行下面的命令就可以添加我們的倉庫:
yum install yum-utils -y
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
然后就可以像下面這樣安裝軟件包,比如 openresty:
yum install openresty -y
如果你想安裝命令行工具 resty,那么可以像下面這樣安裝 openresty-resty 包:
yum install openresty-resty -y
命令行工具 opm 在 openresty-opm 包里,而 restydoc 工具在 openresty-doc 包里頭。
列出所有 openresty 倉庫里頭的軟件包:
yum --disablerepo="*" --enablerepo="openresty" list available
參考 OpenResty RPM 包頁面獲取這些包更多的細節。
五.源碼包編譯安裝
下載
從下載頁 Download下載最新的 OpenResty® 源碼包,並且像下面的示例一樣將其解壓:
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
安裝前的准備
必須將這些庫 perl 5.6.1+, libpcre, libssl安裝在您的電腦之中。 對於 Linux來說, 您需要確認使用 ldconfig 命令,讓其在您的系統環境路徑中能找到它們。
推薦您使用yum安裝以下的開發庫:
yum install pcre-devel openssl-devel gcc curl -y
安裝
tar -zxvf openresty-1.13.6.2.tar.gz
cd openresty-1.13.6.2
./configure
make
make install
如果您的電腦支持多核 make 工作的特性, 您可以這樣編譯:
make -j2
默認, --prefix=/usr/local/openresty 程序會被安裝到/usr/local/openresty目錄。您可以指定各種選項,比如:
./configure --prefix=/opt/openresty \
--with-luajit \
--without-http_redis2_module \
--with-http_iconv_module \
--with-http_postgres_module
試着使用 ./configure --help 查看更多的選項。
六.配置
第一種常規配置方案
修改nginx.conf配置文件
cd /usr/local/openresty/nginx/conf
mv nginx.conf nginx.conf.$(date +%Y%m%d)
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 服務
curl http://localhost:8080/
如果一切正常,我們應該得到輸出
<p>hello, world</p>
重啟 Web 服務
nginx -s reload
第二種lua配置方案
添加lua.conf配置文件
server {
listen 8080;
location / {
default_type text/html;
content_by_lua ' ngx.say("<p>hello, world Lua!</p>") ';
}
}
修改nginx.conf配置文件
cd /usr/local/openresty/nginx/conf
mv nginx.conf nginx.conf.$(date +%Y%m%d)
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 服務
curl http://localhost:8080/
如果一切正常,我們應該得到輸出
<p>hello, world Lua!</p>
配置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: Apache #服務器軟件
Server Hostname: localhost #域名
Server Port: 80 #請求端口號
Document Path: / #文件路徑
Document Length: 40888 bytes #頁面字節數
Concurrency Level: 10 #請求的並發數
Time taken for tests: 27.300 seconds #總訪問時間
Complete requests: 1000 #請求成功數量
Failed requests: 0 #請求失敗數量
Write errors: 0
Total transferred: 41054242 bytes #請求總數據大小(包括header頭信息)
HTML transferred: 40888000 bytes #html頁面實際總字節數
Requests per second: 36.63 [#/sec] (mean) #每秒多少請求,這個是非常重要的參數數值,服務器的吞吐量
Time per request: 272.998 [ms] (mean) #用戶平均請求等待時間
Time per request: 27.300 [ms] (mean, across all concurrent requests)
# 服務器平均處理時間,也就是服務器吞吐量的倒數
Transfer rate: 1468.58 [Kbytes/sec] received #每秒獲取的數據長度
Connection Times (ms)
min mean[+/-sd] median max
Connect: 43 47 2.4 47 53
Processing: 189 224 40.7 215 895
Waiting: 102 128 38.6 118 794
Total: 233 270 41.3 263 945
Percentage of the requests served within a certain time (ms)
50% 263 #50%用戶請求在263ms內返回
66% 271 #66%用戶請求在271ms內返回
75% 279 #75%用戶請求在279ms內返回
80% 285 #80%用戶請求在285ms內返回
90% 303 #90%用戶請求在303ms內返回
95% 320 #95%用戶請求在320ms內返回
98% 341 #98%用戶請求在341ms內返回
99% 373 #99%用戶請求在373ms內返回
100% 945 (longest request)
八.啟動腳本
添加啟動腳本
vim /etc/init.d/openresty
腳本內容如下
#!/bin/sh
#
# openresty - this script starts and stops the openresty daemin
#
# chkconfig: - 85 15
# description: OpenResty is a full-fledged web platform that integrates \
# the standard Nginx core, LuaJIT, many carefully written Lua libraries, \
# lots of high quality 3rd-party Nginx modules, and most of their external dependencies.
# processname: openresty
# config: /usr/local/openresty/nginx/conf/nginx.conf
# pidfile: /usr/local/openresty/nginx/logs/nginx.pid
# Url http://www.cnblogs.com/wushuaishuai/
# Last Updated 2018.07.15
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/openresty/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/openresty/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/openresty/nginx/logs/nginx.pid"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
#service php-fpm start
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
$nginx -s stop
echo_success
retval=$?
echo
#service php-fpm stop
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
$nginx -s reload
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
賦予執行權限
chmod u+x /etc/init.d/openresty
九.使用systemctl來管理openresty
#查看openresty狀態
systemctl status openresty.service
#啟動openresty
systemctl start openresty.service
#設置openresty開機自啟動
systemctl enable openresty.service
#重啟openresty
systemctl restart openresty.service
#停止openresty
systemctl stop openresty.service
#取消openresty開機自啟動
systemctl disable openresty.service