Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

OpenResty- Core implementation Phase

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

The first two articles introduce the core concepts, advantages and architecture of OpenResty, respectively. It is recommended to watch it at least once before this article.

OpenResty- Enterprise Theory and practice OpenResty- Enterprise introduction practice background

The previous chapter introduced that OpenResty is a development platform based on Nginx.

This article continues to introduce the main features of the basic platform (Nginx).

Feature execution phase

OpenResty divides the application into 4 large phases and 11 small phases, as shown in the following figure.

Initialization phase: master process start preload / generate worker process preload forwarding / access phase: url forwarding, permission judgment content processing / generation phase: content generation log phase: log recording

Commonly used in the development of 7-stage set_by_lua*: process branches to determine variables initialization rewrite_by_lua*: forwarding, redirection, caching and other functions (such as specific request proxy to the public network) access_by_lua*: IP admission, Centralized handling of situations such as interface permissions (for example, cooperating with iptable to complete a simple firewall) content_by_lua*: content generation header_filter_by_lua*: response header filtering processing (e.g. adding header information) body_filter_by_lua*: response body filtering processing (e.g. completion of responses in uppercase) Local asynchronous logging is completed after the completion of the log_by_lua*: session (the log can be recorded locally Can also be synchronized to other machines) Test [root@localhost] # cat / etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@localhost ~] # uname-r3.10.0-693.el7.x86_64OpenResty version [root@localhost ~] # / usr/local/openresty/bin/openresty-vnginx version: openresty/1.13.6.2 create a test project directory [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' write configuration file

Output error level log to file via ngx.log

[root@localhost ~] # cat openresty-phase-test/conf/nginx.conf worker_processes 1; # set the number of worker error_log logs/error.log; # specify the error log file path events {worker_connections 1024; # the maximum number of simultaneous external connections allowed by a single worker process} http {server {listen 9999 # set listening port and note 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 (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")}} run the application [root@localhost ~] # / usr/local/openresty/bin/openresty-p openresty-phase-test through openresty to view the contents of the error log file [root@localhost ~] # cat openresty-phase-test/logs/error.log [root@localhost ~] #

The contents of the error log file are empty at this time

Initiate a test request through the curl tool [locally Port is 9999] [root@localhost ~] # curl 127.0.0.1 error 9999 in the configuration file to view the contents of the error log file at 05:34:22 on 2019-08-02 [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 rewrite_by_lua 9999" on 2019-08-02 05:34:22 [error] 1092: * 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 access_by_lua 9999" on 2019-08-02 05:34:22 [error] 1092: * 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 content_by_lua 9999" on 2019-08-02 05:34:22 [error] 1092: * 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 header_filter_by_lua:2 9999" on 2019-08-02 05:34:22 [error] 1092: * 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 body_filter_by_lua:2 9999" on 2019-08-02 05:34:22 [error] 1092: * 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 log_by_lua 9999" 05:34:22 on 2019-08-02 [error] 1092: * 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 log_by_lua"

You can see that the output is arranged in the order of phases.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report