Foreword
The first two articles introduce the core concepts and advantages and architecture of OpenResty respectively. It is recommended to watch it at least once before proceeding with this article.
- OpenResty——–Enterprise-level theoretical and practical articles
- OpenResty——–Enterprise-level introductory practical articles
< /ul>
Background
The previous chapter introduced OpenResty as a development platform based on Nginx.
This article will continue to introduce the basic platform (Nginx) Main features.
Features
Execution phase
OpenResty divides the application into 4 large phases and 11 small phases, as shown in the figure below .
- Initialization phase: master process starts pre-loading/generates worker process pre-loading
- Forwarding/access stage: URL forwarding, authority judgment
- Content processing /Generation stage: content generation
- Log stage: logging
7 stages commonly used in development
- set_by_lua*: process branch processing judgment variable initialization
- rewrite_by_lua*: forwarding, redirection, Caching and other functions (such as proxying specific requests to the external network)
- access_by_lua*: centralized processing of IP access, interface permissions, etc. (such as cooperating with iptable to complete a simple firewall)
- content_by_lua*: Content generation
- header_filter_by_lua*: response header filtering processing (for example, adding header information)
- body_filter_by_lua*: response body filtering processing (for example, complete response content is unified into uppercase)
- log_by_lua*: After the session is completed, log records are completed asynchronously locally (logs can be recorded locally or synchronized to other machines)
Test [commonly used 7 stages ]
Environment
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[[ email protected] ~]# uname -r
3.10.0-693.el7.x86_64
OpenResty version
[[email protected] ~]# /opt/openresty-1.13.6.2/bin/openresty -v
nginx version: openresty/1.13.6.2
Create test project directory
[ [email protected] ~]# 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'
Write configuration file
Output error level logs to file through ngx.log Medium
[[email protected] ~]# cat openresty-phase-test/conf/nginx.conf
worker_processes 1; # Set the number of workers
error_log logs/ error.log; # Specify the error log file path
events {
worker_connections 1024; # The maximum number of external connections that a single worker process is allowed to establish at the same time
}
http {
server {
listen 9999; # Set the listening port, pay attention to whether other services of the system have occupied the port
location / {
set_by_lua_block $a {
ngx.log(ngx.ERR, "my is set_by_lua_block phase")
}
rewrite_by_lua_block {
ngx.log(ng x.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")
}
}
}
}
Run the application via openresty
[[email Protected] ~]# /opt/openresty-1.13.6.2/bin/openresty -p openresty-phase-test
View the content of the error log file
[[emailprotected] ~]# cat openresty-phase-tes t/logs/error.log
[[email protected] ~]#
The content of the error log file is empty at this time
via curl The tool initiates a test request [local, port is 9999 in the configuration file]
[[email protected] ~]# curl 127.0.0.1:9999
View the content of the error log file
h3>
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] 109 2#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"
You can see the sequence of arranging phases for output