前言
前兩篇分別介紹了OpenResty核心概念和,優勢與架構等信息,進行本篇之前建議至少觀看一遍。
背景
之前篇章介紹了OpenResty是基於Nginx爲基礎核心的開發平臺,
本篇將繼續介紹基礎平臺(Nginx)的主要特性。
特性
執行階段
OpenResty將應用分爲4個大階段,11個小階段,如下圖所示。
- 初始化階段: master進程啓動預加載/生成worker進程預加載
- 轉發/訪問階段:url轉發,權限判斷
- 內容處理/生成階段: 內容生成
- 日誌階段: 日誌記錄
開發中常用的7階段
- 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*: 會話完成後本地異步完成日誌記錄(日誌可以記錄在本地,還可以同步到其他機器)
測試[常用的7階段]
環境
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@localhost ~]# uname -r
3.10.0-693.el7.x86_64
OpenResty版本
[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")
}
}
}
}
通過openresty運行應用
[root@localhost ~]# /usr/local/openresty/bin/openresty -p openresty-phase-test
查看錯誤日誌文件內容
[root@localhost ~]# cat openresty-phase-test/logs/error.log
[root@localhost ~]#
此時錯誤日誌文件內容爲空
通過curl工具發起測試請求[本地,端口爲配置文件中的9999]
[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"
可以看到安排階段順序進行輸出