您好,登錄后才能下訂單哦!
前兩篇分別介紹了OpenResty核心概念和,優(yōu)勢(shì)與架構(gòu)等信息,進(jìn)行本篇之前建議至少觀看一遍。
- OpenResty--------企業(yè)級(jí)理論實(shí)踐篇
- OpenResty--------企業(yè)級(jí)入門(mén)實(shí)踐篇
之前篇章介紹了OpenResty是基于Nginx為基礎(chǔ)核心的開(kāi)發(fā)平臺(tái),
本篇將繼續(xù)介紹基礎(chǔ)平臺(tái)(Nginx)的主要特性。
OpenResty將應(yīng)用分為4個(gè)大階段,11個(gè)小階段,如下圖所示。
- 初始化階段: master進(jìn)程啟動(dòng)預(yù)加載/生成worker進(jìn)程預(yù)加載
- 轉(zhuǎn)發(fā)/訪問(wèn)階段:url轉(zhuǎn)發(fā),權(quán)限判斷
- 內(nèi)容處理/生成階段: 內(nèi)容生成
- 日志階段: 日志記錄
- set_by_lua*: 流程分支處理判斷變量初始化
- rewrite_by_lua*: 轉(zhuǎn)發(fā)、重定向、緩存等功能(例如特定請(qǐng)求代理到外網(wǎng))
- access_by_lua*: IP 準(zhǔn)入、接口權(quán)限等情況集中處理(例如配合 iptable 完成簡(jiǎn)單防火墻)
- content_by_lua*: 內(nèi)容生成
- header_filter_by_lua*: 響應(yīng)頭部過(guò)濾處理(例如添加頭部信息)
- body_filter_by_lua*: 響應(yīng)體過(guò)濾處理(例如完成應(yīng)答內(nèi)容統(tǒng)一成大寫(xiě))
- log_by_lua*: 會(huì)話(huà)完成后本地異步完成日志記錄(日志可以記錄在本地,還可以同步到其他機(jī)器)
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@localhost ~]# uname -r
3.10.0-693.el7.x86_64
[root@localhost ~]# /usr/local/openresty/bin/openresty -v
nginx version: openresty/1.13.6.2
[root@localhost ~]# mkdir -vp openresty-phase-test/{conf,logs}
mkdir: created directory ‘openresty-phase-test’
mkdir: created directory ‘openresty-phase-test/conf’
mkdir: created directory ‘openresty-phase-test/logs’
通過(guò)ngx.log輸出錯(cuò)誤級(jí)別日志至文件中
[root@localhost ~]# cat openresty-phase-test/conf/nginx.conf
worker_processes 1; # 設(shè)置worker數(shù)量
error_log logs/error.log; # 指定錯(cuò)誤日志文件路徑
events {
worker_connections 1024; # 單個(gè)worker進(jìn)程最大允許同時(shí)建立外部連接的數(shù)量
}
http {
server {
listen 9999; # 設(shè)置監(jiān)聽(tīng)端口, 注意系統(tǒng)其它服務(wù)是否已占用該端口
location / {
set_by_lua_block $a {
ngx.log(ngx.ERR, "my is set_by_lua_block phase")
}
rewrite_by_lua_block {
ngx.log(ngx.ERR, "my is rewrite_by_lua_block phase")
}
access_by_lua_block {
ngx.log(ngx.ERR, "my is access_by_lua_block phase")
}
content_by_lua_block {
ngx.log(ngx.ERR, "my is content_by_lua_block phase")
}
header_filter_by_lua_block {
ngx.log(ngx.ERR, "my is header_filter_by_lua_block phase")
}
body_filter_by_lua_block {
ngx.log(ngx.ERR, "my is body_filter_by_lua_block phase")
}
log_by_lua_block {
ngx.log(ngx.ERR, "my is log_by_lua_block phase")
}
}
}
}
[root@localhost ~]# /usr/local/openresty/bin/openresty -p openresty-phase-test
[root@localhost ~]# cat openresty-phase-test/logs/error.log
[root@localhost ~]#
此時(shí)錯(cuò)誤日志文件內(nèi)容為空
[root@localhost ~]# curl 127.0.0.1:9999
2019/08/02 05:34:22 [error] 1092#0: *1 [lua] set_by_lua:2: my is set_by_lua_block phase, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:9999"
2019/08/02 05:34:22 [error] 1092#0: *1 [lua] rewrite_by_lua(nginx.conf:18):2: my is rewrite_by_lua_block phase, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:9999"
2019/08/02 05:34:22 [error] 1092#0: *1 [lua] access_by_lua(nginx.conf:22):2: my is access_by_lua_block phase, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:9999"
2019/08/02 05:34:22 [error] 1092#0: *1 [lua] content_by_lua(nginx.conf:27):2: my is content_by_lua_block phase, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:9999"
2019/08/02 05:34:22 [error] 1092#0: *1 [lua] header_filter_by_lua:2: my is header_filter_by_lua_block phase, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:9999"
2019/08/02 05:34:22 [error] 1092#0: *1 [lua] body_filter_by_lua:2: my is body_filter_by_lua_block phase, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:9999"
2019/08/02 05:34:22 [error] 1092#0: *1 [lua] log_by_lua(nginx.conf:39):2: my is log_by_lua_block phase while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:9999"
可以看到安排階段順序進(jìn)行輸出
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。