您好,登錄后才能下訂單哦!
前兩篇分別介紹了OpenResty核心概念和,優勢與架構等信息,進行本篇之前建議至少觀看一遍。
- OpenResty--------企業級理論實踐篇
- OpenResty--------企業級入門實踐篇
之前篇章介紹了OpenResty是基于Nginx為基礎核心的開發平臺,
本篇將繼續介紹基礎平臺(Nginx)的主要特性。
OpenResty將應用分為4個大階段,11個小階段,如下圖所示。
- 初始化階段: master進程啟動預加載/生成worker進程預加載
- 轉發/訪問階段:url轉發,權限判斷
- 內容處理/生成階段: 內容生成
- 日志階段: 日志記錄
- set_by_lua*: 流程分支處理判斷變量初始化
- rewrite_by_lua*: 轉發、重定向、緩存等功能(例如特定請求代理到外網)
- access_by_lua*: IP 準入、接口權限等情況集中處理(例如配合 iptable 完成簡單防火墻)
- content_by_lua*: 內容生成
- header_filter_by_lua*: 響應頭部過濾處理(例如添加頭部信息)
- body_filter_by_lua*: 響應體過濾處理(例如完成應答內容統一成大寫)
- log_by_lua*: 會話完成后本地異步完成日志記錄(日志可以記錄在本地,還可以同步到其他機器)
[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’
通過ngx.log輸出錯誤級別日志至文件中
[root@localhost ~]# cat openresty-phase-test/conf/nginx.conf
worker_processes 1; # 設置worker數量
error_log logs/error.log; # 指定錯誤日志文件路徑
events {
worker_connections 1024; # 單個worker進程最大允許同時建立外部連接的數量
}
http {
server {
listen 9999; # 設置監聽端口, 注意系統其它服務是否已占用該端口
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 ~]#
此時錯誤日志文件內容為空
[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"
可以看到安排階段順序進行輸出
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。