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

发布时间:2020-07-25 21:38:22 作者:asd1123509133
来源:网络 阅读:3357

前言

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

背景

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

特性

执行阶段

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

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

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

开发中常用的7阶段

测试[常用的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"

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

推荐阅读:
  1. OpenResty--------企业级入门实践篇
  2. OpenResty--------企业级理论实践篇

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

openresty core nginx

上一篇:华为二面心得 Take IT Easy!

下一篇:cocos2dx番外篇——虚拟摇杆

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》