- 簡介
OpenResty又被稱為ngx_openresty,基於nginx核心和許多第三方模塊組成,默認繼承了Lua開發環境,使得nginx可以做為一個web服務使用
- 原理
Nginx采用master-worker模型,也就是一個master進程管理多個worker進程,基本的時間處理都放在worker進程中,master進程負責全局初始化以及對worker進程的管理,OpenResty利用 nginx的非阻塞IO模型,實現高性能web應用程序
- 安裝
- 安裝依賴
依賴pcre做正則引擎,openssl做安全引擎
1 yum install -y gcc gcc-c++ zlib-devel pcre pcre-devel openssl openssl-devel readline-devel
2. 版本
openresty-1.15.8.2目前為最高版本,下載地址為:http://openresty.org/cn/download.html
3. 第三方模塊介紹
openresty集成的第三方模塊放在openresty-1.15.8.2/build 目錄下,默認除了lua相關的其他模塊,編譯安裝時會自動安裝
1 [root@localhost build]# pwd 2 /root/qin/openresty-1.15.8.2/build 3 [root@localhost build]# ll 4 total 256 5 drwxrwxr-x. 5 mysql 1003 117 Aug 29 13:33 array-var-nginx-module-0.05 6 drwxrwxr-x. 6 mysql 1003 162 Aug 29 13:33 drizzle-nginx-module-0.1.11 7 drwxrwxr-x. 5 mysql 1003 151 Aug 29 13:33 echo-nginx-module-0.61 8 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 encrypted-session-nginx-module-0.08 9 drwxrwxr-x. 5 mysql 1003 95 Aug 29 13:33 form-input-nginx-module-0.12 10 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 headers-more-nginx-module-0.33 11 drwxrwxr-x. 5 mysql 1003 95 Aug 29 13:33 iconv-nginx-module-0.14 12 -rwxrwxr-x. 1 mysql 1003 1502 Aug 29 13:32 install 13 drwxrwxr-x. 4 mysql 1003 4096 Dec 18 15:11 lua-cjson-2.1.0.7 14 drwxrwxr-x. 7 mysql 1003 146 Aug 29 13:33 LuaJIT-2.1-20190507 15 drwxr-xr-x. 3 root root 18 Dec 18 15:10 luajit-root 16 drwxrwxr-x. 4 mysql 1003 91 Dec 18 15:11 lua-rds-parser-0.06 17 drwxrwxr-x. 3 mysql 1003 167 Dec 18 15:11 lua-redis-parser-0.13 18 drwxrwxr-x. 5 mysql 1003 176 Aug 29 13:33 lua-resty-core-0.1.17 19 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-dns-0.21 20 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-limit-traffic-0.06 21 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-lock-0.08 22 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-lrucache-0.09 23 drwxrwxr-x. 4 mysql 1003 107 Aug 29 13:33 lua-resty-memcached-0.14 24 drwxrwxr-x. 5 mysql 1003 157 Aug 29 13:33 lua-resty-mysql-0.21 25 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-redis-0.27 26 drwxrwxr-x. 4 mysql 1003 104 Aug 29 13:33 lua-resty-shell-0.02 27 drwxrwxr-x. 4 mysql 1003 173 Dec 18 15:11 lua-resty-signal-0.02 28 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-string-0.11 29 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-upload-0.10 30 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-upstream-healthcheck-0.06 31 drwxrwxr-x. 4 mysql 1003 142 Aug 29 13:33 lua-resty-websocket-0.07 32 drwxrwxr-x. 3 mysql 1003 51 Aug 29 13:33 lua-tablepool-0.01 33 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 memc-nginx-module-0.19 34 drwxrwxr-x. 9 mysql 1003 139 Dec 18 15:10 nginx-1.15.8 35 -rw-rw-r--. 1 mysql 1003 14799 Aug 29 13:32 nginx-no_pool.patch 36 drwxr-xr-x. 6 root root 4096 Dec 18 14:42 nginx_upstream_check_module-0.3.0 37 drwxr-xr-x. 3 root root 123 Dec 18 14:42 ngx_cache_purge-2.3 38 drwxrwxr-x. 4 mysql 1003 127 Apr 3 2015 ngx_coolkit-0.2 39 drwxrwxr-x. 9 mysql 1003 204 Aug 29 13:33 ngx_devel_kit-0.3.1rc1 40 drwxrwxr-x. 10 mysql 1003 202 Aug 29 13:33 ngx_lua-0.10.15 41 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 ngx_lua_upstream-0.07 42 drwxrwxr-x. 4 mysql 1003 113 Aug 29 13:33 ngx_postgres-1.0 43 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 ngx_stream_lua-0.0.7 44 drwxrwxr-x. 5 mysql 1003 90 Aug 29 13:33 opm-0.0.5 45 drwxrwxr-x. 47 mysql 1003 4096 Aug 29 13:33 pod 46 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 rds-csv-nginx-module-0.09 47 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 rds-json-nginx-module-0.15 48 drwxrwxr-x. 6 mysql 1003 163 Aug 29 13:33 redis2-nginx-module-0.15 49 drwxr-xr-x. 3 mysql 1003 154 Aug 29 13:32 redis-nginx-module-0.3.7 50 drwxrwxr-x. 4 mysql 1003 110 Aug 29 13:33 resty-cli-0.24 51 -rw-rw-r--. 1 mysql 1003 226755 Aug 29 13:33 resty.index 52 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 set-misc-nginx-module-0.32 53 drwxrwxr-x. 5 mysql 1003 132 Aug 29 13:33 srcache-nginx-module-0.31 54 drwxrwxr-x. 5 mysql 1003 136 Aug 29 13:33 xss-nginx-module-0.06
說明:
內置模塊 --with-<module>
--with-cc-opt=-O2
#編譯器優化
--with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -ljemalloc
#添加luajit的的lib目錄,並使用ljemalloc來管理內存,性能可提高30%
--user=www --group=www
#用戶,組,不解釋
--with-http_stub_status_module
#監控模塊,不解釋
--with-http_v2_module
#使Nginx支持http2,由於http2是趨勢,所以這里加上,方便以后擴展。
--with-http_ssl_module
#SSL不解釋
--with-ipv6
#支持ipv6,由於ipv6是趨勢,所以這里加上,方便以后擴展
--with-http_gzip_static_module
#gzip不解釋
--with-http_realip_module
#realip,必備模塊,取用戶真實ip模塊
--with-http_flv_module
#支持flv流媒體的各項擴展參數
--with-pcre=/root/oneinstack/src/openresty-1.9.15.1/../pcre-8.38
#加載pcre,不解釋
--with-pcre-jit
#加載pcre模塊
第三方模塊 --add-module
--add-module=../redis2-nginx-module
#使 nginx 可以直接讀 redis 中緩存的文件內容
--add-module=../redis2-nginx-module
#是一個支持 Redis 2.0 協議的 Nginx upstream 模塊,它可以讓 Nginx 以非阻塞方式直接防問遠方的 Redis 服務,同時支持 TCP 協議和 Unix Domain Socket 模式,並且可以啟用強大的 Redis 連接池功能
--add-module=../ngx_devel_kit-0.3.0
#nginx開發包
--add-module=../echo-nginx-module-0.59
#可以使Nginx輸出一些簡單的文字
--add-module=../xss-nginx-module-0.05
#緩存有callback的數據
--add-module=../ngx_coolkit-0.2rc3
#nginx的插件集合,方便擴展
--add-module=../set-misc-nginx-module-0.30
#set-misc-nginx-module模塊是標准的HttpRewriteModule指令的擴展,提供更多的功能,
#如URI轉義與非轉義、JSON引述、Hexadecimal/MD5/SHA1/Base32/Base64編碼與解碼、隨機數等等。
--add-module=../form-input-nginx-module-0.12
#NGINX模塊讀取HTTP POST和PUT
--add-module=../encrypted-session-nginx-module-0.05
#加密cookie
--add-module=../srcache-nginx-module-0.31
#srcache是為location增加了透明的基於subrequest的緩存層,與其它模塊配合,可實現高效緩存
--add-module=../ngx_lua-0.10.5
#nginx lua模塊,不解釋
--add-module=../ngx_lua_upstream-0.05
#Nginx LUA API
--add-module=../headers-more-nginx-module-0.30
#定義header的返回信息
--add-module=../array-var-nginx-module-0.05
#Nginx添加支持數組類型的變量
--add-module=../memc-nginx-module-0.17
#使緩存支持高並發,針對一些訪問量很大的頁面,特別是瞬時訪問量很大的系統,jetty無法支撐住請求,這時候可以使用ngx-memc模塊與srcache-nginx-module模塊將請求頁面數據
存放在 memcached中,可大大提升系統的並發能力
--add-module=../redis2-nginx-module-0.13
#它可以讓 Nginx 以非阻塞方式直接防問遠方的 Redis 服務,同時支持 TCP 協議和 Unix Domain Socket 模式,並且可以啟用強大的 Redis 連接池功能。
--add-module=../redis-nginx-module-0.3.7
#讓Nginx可直接查詢redis中的數據,不用經過程序,大大的提高效率。
--add-module=../rds-json-nginx-module-0.14
#用於處理json字符串,提升nginx處理json字符串性能
--add-module=../rds-csv-nginx-module-0.07
#控制輸出的模塊轉換Resty-DBD流為csv
openresty沒有的模塊
--add-module=../nginx_upstream_check_module-0.3.0
#該模塊用於upstream健康檢查
#下載地址:https://codeload.github.com/yaoweibin/nginx_upstream_check_module/tar.gz/v0.3.0
--add-module=../ngx_cache_purge-2.3
#該模塊用於清理nginx緩存
#下載地址:https://codeload.github.com/FRiCKLE/ngx_cache_purge/tar.gz/2.3
lua開發環境相關模塊,安裝方式如下:
1 [root@localhost LuaJIT-2.1-20190507]# pwd 2 /root/qin/openresty-1.15.8.2/build/LuaJIT-2.1-20190507 3 [root@localhost LuaJIT-2.1-20190507]# ll 4 total 36 5 -rw-rw-r--. 1 mysql 1003 3053 May 3 2019 COPYRIGHT 6 drwxrwxr-x. 3 mysql 1003 4096 May 3 2019 doc 7 drwxrwxr-x. 2 mysql 1003 4096 May 3 2019 dynasm 8 drwxrwxr-x. 2 mysql 1003 39 Dec 18 15:10 etc 9 -rw-rw-r--. 1 mysql 1003 5684 May 3 2019 Makefile 10 -rw-rw-r--. 1 mysql 1003 453 May 3 2019 README 11 drwxrwxr-x. 5 mysql 1003 8192 Dec 18 15:10 src 12 drwxrwxr-x. 2 mysql 1003 112 May 3 2019 t 13 [root@localhost LuaJIT-2.1-20190507]# make && make install 14 ........ 15 ........ 16 ln -sf luajit-2.1.0-beta3 /usr/local/bin/luajit 17 ==== Successfully installed LuaJIT 2.1.0-beta3 to /usr/local ====
4. 安裝步驟
1 [root@localhost openresty-1.15.8.2]# pwd 2 /root/qin/openresty-1.15.8.2 3 [root@localhost openresty-1.15.8.2]# ./configure --prefix=/data/soft/openresty/nginx --with-http_ssl_module --with-http_stub_status_module --with-ipv6 --with-http_realip_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --add-module=/root/qin^Cginx_upstream_check_module-0.3.0 --add-module=/root/qin/ngx_cache_purge-2.3 4 5 ........... 6 ........... 7 8 ./configure: warning: the "--with-ipv6" option is deprecated 9 cd ../.. 10 Type the following commands to build and install: 11 gmake 12 gmake install 13 14 [root@localhost openresty-1.15.8.2]# gmake && gmake install 15 16 ........ 17 ........ 18 mkdir -p /data/soft/openresty/nginx/site/lualib /data/soft/openresty/nginx/site/pod /data/soft/openresty/nginx/site/manifest 19 ln -sf /data/soft/openresty/nginx/nginx/sbin/nginx /data/soft/openresty/nginx/bin/openresty
查看openresty版本以及已安裝的模塊
1 [root@localhost openresty-1.15.8.2]# /data/soft/openresty/nginx/bin/openresty -V 2 nginx version: openresty/1.15.8.2 3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 4 built with OpenSSL 1.0.2k-fips 26 Jan 2017 5 TLS SNI support enabled 6 configure arguments: --prefix=/data/soft/openresty/nginx/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1rc1 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.15 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.7 --with-ld-opt=-Wl,-rpath,/data/soft/openresty/nginx/luajit/lib --with-http_ssl_module --with-http_stub_status_module --with-ipv6 --with-http_realip_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --add-module=/home/pukka/pkg/nginx_upstream_check_module-0.3.0 --add-module=/home/pukka/pkg/ngx_cache_purge-2.3 --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module
直接configure安裝的模塊
1 [root@localhost openresty-1.15.8.2]# /data/soft/openresty/nginx/bin/openresty -V 2 nginx version: openresty/1.15.8.2 3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 4 built with OpenSSL 1.0.2k-fips 26 Jan 2017 5 TLS SNI support enabled 6 configure arguments: --prefix=/data/soft/openresty/nginx/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1rc1 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.15 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.7 --with-ld-opt=-Wl,-rpath,/data/soft/openresty/nginx/luajit/lib --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module
openresty啟動
1 [root@localhost openresty-1.15.8.2]# /data/soft/openresty/nginx/bin/openresty 2 [root@localhost openresty-1.15.8.2]# ps -ef | grep nginx 3 root 13973 1 0 17:14 ? 00:00:00 nginx: master process /data/soft/openresty/nginx/bin/openresty 4 nobody 13974 13973 0 17:14 ? 00:00:00 nginx: worker process 5 root 13980 1431 0 17:14 pts/0 00:00:00 grep --color=auto nginx