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

How to extend Nginx through Lua

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to expand Nginx through Lua". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to expand Nginx through Lua".

1. Ngx_lua module

Nginx module needs to be developed in C, and must comply with a series of complex rules. The most important module developed in C must be familiar with the source code of Nginx, which makes developers afraid of it.

By integrating the lua interpreter into Nginx, the ngx_lua module can use lua scripts to implement business logic.

The module has the following characteristics:

Handle requests with high concurrency and non-blocking.

Lua has built-in co-routines so that asynchronous callbacks can be well converted into sequential calls.

Each co-program has an independent global environment (variable space), which inherits from the globally shared, read-only "comman data".

Thanks to the support of the Lua protocol, ngx_lua requires very little memory when processing 10000 concurrent requests. According to the test, ngx_lua needs only 2KB memory to process each request, and even less if LuaJIT is used.

Ngx_lua is ideal for implementing scalable, highly concurrent services.

two。 Synergetics (Coroutine) 1. The co-program is similar to multithreading, which is different from multithreading.

The co-program is not an os thread, so the overhead of creation and switching is relatively less than that of the thread.

Like a thread, a co-program has its own stack, local variables, etc., but the stack of the co-program is simulated in the user process space, so the creation and overhead is very small.

Multithreaded programs are executed concurrently by multiple threads, that is, multiple control flows are executed in an instant. The cooperative process emphasizes the cooperative relationship among multiple collaborative processes, and only when one collaborative process gives up the execution power actively, the other collaborative process can obtain the executive power, so at a certain moment, only one of the multiple collaborative processes is running.

Since only one of the multiple protocols is running, access to the critical section does not need to be locked, while in the case of multithreading, it must be locked.

Multi-threaded programs have multiple control flows, so the behavior of the program is uncontrollable, and the execution of multiple coprograms is defined by the developer, so it is controllable.

Each Worker process of Nginx is encapsulated into a co-program based on an event model such as epoll or kqueue, and each request has a co-program for processing. This is exactly consistent with the model of Lua's built-in co-program, so even if ngx_lua needs to execute lua, it has some overhead relative to C, but it still ensures high concurrency.

3. Nginx process model

Nginx adopts multi-process model, and single Master- multi-Worker,Master process is mainly used to manage Worker process.

The Worker process uses a single-threaded, non-blocking event model (Event Loop, event loop) to monitor the port and process and respond to client requests. At the same time, Worker also deals with signals from Master. The number of Worker processes is generally set to the number of machine CPU cores.

1. The Master process includes the following four main functions

Receive signals from the outside world.

Signals are sent to each Worker process.

Monitor the running status of the Worker process.

When the Worker process exits (in exceptional cases), the new Worker process is automatically restarted.

two。 Process model

4. HTTP request processing phase describes the post-read request content stage. Immediately after nginx reads and parses the request header, it starts to run the server-rewriteserver request address rewriting phase find-config configuration lookup phase, which is used to complete the configuration between the current request and the location configuration block. Rewritelocation request address rewriting phase. When the ngx_rewrite instruction is used in location, it is the post-rewrite request address rewriting phase that runs at this stage. When nginx completes the internal jump required by the rewrite phase, if there is such a requirement in the rewrite phase, the preaccess access check preparatory phase, ngx_limit_req and ngx_limit_zone run at this stage, ngx_limit_req can control the access frequency of the request, and ngx_limit_zone can control the concurrency of the access. In the access permission check phase, ngx_access runs at this stage, and most of the configuration instructions are to perform tasks related to access control, to check the user's access rights, to check whether the user's source IP is legal, to check the post-access access rights, to check the submission stage, try-files configuration try_files processing phase, content content generation phase, which is the most important stage of all request processing stages. Because the instructions at this stage are usually used to generate HTTP response content of the log log module processing stage 5. Ngx_lua instruction

Ngx_lua is part of nginx, and its execution instructions are included in the 11 steps of nginx. The corresponding processing phase can do plug-in processing, that is, pluggable architecture, but ngx_lua does not run in all stages; in addition, instructions can be configured in http, server, server if, location, and location if.

The processing phase of the instruction uses scope to explain when the init_by_luainit_by_lua_fileloading-confighttpnginx Master process loads the configuration; it is usually used to initialize the global configuration / preload the Lua module init_worker_by_luainit_worker_by_lua_filestarting-workerhttp the timer that is called when each Nginx Worker process starts, and only after init_by_\ lua if the Master process is not allowed It is usually used to pull configuration / data regularly, or to check the health of back-end services. Set_by_luaset_by_lua_filerewriteserver,server if,location,location if sets the nginx variable to implement complex assignment logic; here it is blocking, and the Lua code can implement complex forwarding / redirection logic for very fast write_by_luarewrite_by_lua_filerewrite tailhttp,server,location,location ifrewrite phase processing. Access_by_luaaccess_by_lua_fileaccess tailhttp,server,location,location if request access phase processing, used to access control content_by_luacontent_by_lua_filecontentlocation,location if content processor, accept request processing and output response header_filter_by_luaheader_filter_by_lua_fileoutput-header-firsthttp,server,location,location if settings header and cookiebody_filter_by_luabody_filter_by_lua_fileoutput-body-filterhttp,server,location,location if to filter response data Such as truncation, replacement log_by_lualog_by_lua_fileloghttp,server,location,location iflog phase processing, such as recording visits / statistical average response time 6. OpenResty1. Concept

OpenResty is a high-performance Web platform based on Nginx and Lua, which integrates a large number of excellent Lua libraries, third-party modules and most dependencies. It is used to conveniently build dynamic Web applications, Web services and dynamic gateways that can handle ultra-high concurrency and high scalability.

two。 working principle

OpenResty effectively turns Nginx into a powerful general-purpose Web application platform by bringing together a variety of well-designed Nginx modules (mainly developed by the OpenResty team). In this way, Web developers and system engineers can use Lua scripting language to call various C and Lua modules supported by Nginx to quickly build a high-performance Web application system capable of concurrent connections on a single machine of 10k or more than 1000k.

3. target

The goal of OpenResty is to have your Web service run directly inside the Nginx service, taking full advantage of Nginx's non-blocking IPUP O model to provide consistent, high-performance responses not only to HTTP client requests, but even to remote backends such as MySQL, PostgreSQL, Memcached, and Redis.

7. Ngx_lua instance

Content_by_lua: a content processor that receives request processing and outputs responses.

This instruction works in the content phase of the Nginx processing process, the content generation phase, which is the most important of all request processing phases, because the instructions in this stage are usually used to generate HTTP response content.

test

Output: $curl http://127.0.0.1/$ Hello,world

1. Simple example user www-data;worker_processes auto;pid / run/nginx.pid;error_log logs/error.log;events {worker_connections 1024;} http {server {listen 8080; location / {default_type text/html; content_by_lua 'ngx.say ("

Hello, world!

")';}} user www-data;worker_processes auto;pid / run/nginx.pid;error_log logs/error.log;events {worker_connections 1024;} http {server {listen 8080; location / {default_type text/html Content_by_lua_block {ngx.say ('Hello, worldview')} launch / usr/local/openresty/nginx/sbin/nginx-p `pwd`-c conf/nginx_openresty_01.conf test curl-I http://127.0.0.1/2. Hibernation example user www-data;worker_processes auto;pid / run/nginx.pid;error_log logs/error.log;events {worker_connections 1024;} http {server {listen 8080; location / lua_1 {default_type text/html; content_by_lua_block {ngx.say ('Hello, world! @ Time 1employees') Ngx.sleep (3) ngx.say ('Hello, world! @ Time 2pm')} launch / usr/local/openresty/nginx/sbin/nginx-p `pwd`-c conf/nginx_openresty_02.conf Test curl-I http://127.0.0.1/lua_1curl-I http://127.0.0.1/lua_13. Example user www-data;worker_processes auto;pid / run/nginx.pid;error_log logs/error.log;events {worker_connections 1024;} http {server {listen 8080; location / lua_1 {default_type text/html with parameters Content_by_lua_block {ngx.say (ngx.var.arg_a)} launch / usr/local/openresty/nginx/sbin/nginx-p `pwd`-c conf/nginx_openresty_03.conf test curl-I http://127.0.0.1?a=nginx_lua8. OpenResty connection redis profile user www-data;worker_processes auto;pid / run/nginx.pid;error_log logs/error.log;events {worker_connections 1024;} http {server {listen 8080; location ~ / redis_lua/ (\ d +) ${default_type text/html; charset utf-8; lua_code_cache on Content_by_lua_file'/ home/zp/openresty/lua/redis.lua' } lua script local json = require ("cjson") local redis = require ("resty.redis") local red = redis:new () red:set_timeout (1000) local ip = "127.0.0.1" local port = 6379local ok, err = red:connect (ip, port) if not ok then ngx.say ('connect to redis error:', err) return ngx.exit (500) endlocal id = ngx.var [1] local value = "calue-".. idred: set (id) Value) local resp, err = red:get (id) if not resp then ngx.say ('get from redis error:' Err) return ngx.exit (500) endred:close () ngx.say (json.encode ({content=resp})) Test launch / usr/local/openresty/nginx/sbin/nginx-p `pwd`-c conf/nginx_openresty_04.conf Test curl-I http://127.0.0.1/redis_lua/1curl-I http://127.0.0.1/redis_lua/2curl-I http://127.0.0.1/redis_lua/3 Thank you for reading The above is the content of "how to expand Nginx through Lua". After the study of this article, I believe you have a deeper understanding of how to expand Nginx through Lua, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report