前两篇分别介绍了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"
可以看到安排阶段顺序进行输出
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。