OpenResty--------核心执行阶段篇

前言

前两篇分别介绍了OpenResty核心概念和,优势与架构等信息,进行本篇之前建议至少观看一遍。

创新互联-专业网站定制、快速模板网站建设、高性价比泰州网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式泰州网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖泰州地区。费用合理售后完善,10余年实体公司更值得信赖。

  • OpenResty--------企业级理论实践篇
  • OpenResty--------企业级入门实践篇

背景

之前篇章介绍了OpenResty是基于Nginx为基础核心的开发平台,
本篇将继续介绍基础平台(Nginx)的主要特性。

特性

执行阶段

OpenResty将应用分为4个大阶段,11个小阶段,如下图所示。

  • 初始化阶段: master进程启动预加载/生成worker进程预加载
  • 转发/访问阶段:url转发,权限判断
  • 内容处理/生成阶段: 内容生成
  • 日志阶段: 日志记录

OpenResty--------核心执行阶段篇

OpenResty--------核心执行阶段篇

开发中常用的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"

可以看到安排阶段顺序进行输出


网页名称:OpenResty--------核心执行阶段篇
文章转载:http://ybzwz.com/article/ippccd.html