前两篇分别介绍了OpenResty核心概念和,优点与架构等信息,进行本篇以前建议至少观看一遍。nginx
以前篇章介绍了OpenResty是基于Nginx为基础核心的开发平台,
本篇将继续介绍基础平台(Nginx)的主要特性。缓存
OpenResty将应用分为4个大阶段,11个小阶段,以下图所示。bash
- 初始化阶段: 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 ~]#
此时错误日志文件内容为空curl
[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"
能够看到安排阶段顺序进行输出异步