Nginx and Lua execution sequence in Linux

Source: Internet
Author: User
Tags lua cloudflare



1.1. Introduce
ngx_lua– embeds the Lua language into Nginx, enabling it to support LUA to quickly develop business logic based on Nginx
The module is not in the Nginx source package, it needs to download and compile the installation itself. Use LUA 5.1 (currently does not support LUA 5.2) or Luajit 2.0.
After adding LUA support, the development of complex modules, the cycle is fast, still 100% asynchronous non-blocking.



Ngx_lua which people are using:
Taobao, Tencent Finance, NetEase Finance, where to go to the network and so on
CloudFlare, CNN, Wingify, Reblaze, Turner, broadcasting System



The main developer of the project:
Chaoslawful Taobao, Alibaba Grp.
Agentzh CloudFlare



Https://github.com/chaoslawful/lua-nginx-module



1.2. Installation
1.2.1. Install the JIT platform
Jit
Usually, the program has two ways of running: Static compilation and dynamic literal translation.
The statically compiled program is translated into machine code before execution, while the dynamic literal translation is performed by one sentence while the other is running.
Just-in-time compilation (Just-In-Time Compiler) mixes the two, compiling the source code in one sentence, but caching the translated code to reduce performance loss.
JAVA,. NET implementations use Just-in-time compilation to provide high-speed code execution.



Attention:
Add "Lua_code_cache On/off" to the nginx.conf to turn on whether to cache code, so each time you change the "*.lua" file, you must reload Nginx to take effect. Valid only for "Set_by_lua_file, Content_by_lua_file, Rewrite_by_lua_file, and Access_by_lua_file" because the other is written in the configuration file. Changes to the code must also be reload Nginx. In a production environment, cache cannot be disabled. Also using "Dofile" or "Loadfie" in LUA code to load an external LUA script will not cache it and should be replaced with "require". Disables cache when and only if it is under the mode code.



Luajit
is a JIT-compiled technology to compile the Lua script into machine code and run by the CPU
Version: 2.0 and 2.1
The current stable version is 2.0.
2.1 for version and Ngx_lua will have a greater performance improvement, mainly CloudFlare company to Luajit donation.
FFI Library is the most important extension library in Luajit.
1. It allows the external C function to be called from pure Lua code, using the C data structure;
2. No longer need to write a LUA extension library like the LUA standard math library;
3. Release developers from the heavy workload of developing the LUA Extension C library (language/functional binding library);



1.3. Nginx and Lua execution order
1.3.1. Nginx Order
Nginx processing Each user request is processed sequentially in several different stages (phase), not according to the order on the configuration file.
The process of Nginx processing requests is divided into 11 phases, followed by the order of execution
Post-read, Server-rewrite, find-config, rewrite, Post-rewrite, preaccess, Access, post-access, try-files, content, log.



Post-read:
Read Request content Phase
Nginx starts running immediately after reading and parsing the request header
For example, the module Ngx_realip registers a handler in the Post-read phase, and its function is to force Nginx to think that the source address of the current request is the value of a specified request header.



Server-rewrite
Server request Address Rewrite phase
When the Ngx_rewrite module's set configuration instructions are written directly in the server configuration block, they are basically run in the server-rewrite phase



Find-config
Configuration Lookup Phase
The Nginx module registration handler is not supported at this stage, but the pairing of the current request to the location configuration block is performed by the Nginx core.



Rewrite
Location Request Address Rewrite phase
When the instruction of the Ngx_rewrite module is used in the location block, it is run at this rewrite stage.
In addition, NGX_SET_MISC (set MD5, ENCODE_BASE64, etc.) module instructions, as well as the Ngx_lua module Set_by_lua instructions and Rewrite_by_lua instructions also at this stage.



Post-rewrite
Request Address rewrite submission phase
The Nginx core completes the "internal jump" operation required by the rewrite phase, if the rewrite phase requires it.



Preaccess
Access permission Check preparation phase
The standard modules Ngx_limit_req and Ngx_limit_zone run at this stage, which controls the frequency of access to the request, which can limit the concurrency of the access.



Access
Access permission Check phase
Standard module ngx_access, third party module Ngx_auth_request, and Third-party module Ngx_lua Access_by_lua instructions run at this stage.
Configuration directives are mostly tasks that perform access control, such as checking the user's access rights, checking the user's source IP address for legality



Post-access
Access permission Check submission phase
It is mainly used to implement the configuration instruction satisfy provided by the standard Ngx_http_core module in the access phase.
Satisfy all (with relationship)
Satisfy any (or relationship)



Try-files
Configuration Item Try_files Processing phase
Features specifically designed to implement the standard configuration Directive try_files
If none of the file system objects corresponding to the previous N-1 parameters exist, the try-files phase immediately initiates the URI specified by the "internal jump" to the last parameter (that is, the nth argument).



Content
Content Generation Phase
The content phase of the Nginx is one of the most important in all request processing stages, because configuration directives that run at this stage generally shoulder the mission of generating "content" and outputting HTTP responses.



Log
Log module processing phase
Record log



Taobao has an open Nginx development manual that contains a lot of useful information



http://tengine.taobao.org/book/



The author's Google Forum:



Https://groups.google.com/forum/#!forum/openresty



1.3.2. Lua Order
Nginx the LUA processing phase and the scope of use:



Init_by_lua http
Set_by_lua server, server if, location, location if
Rewrite_by_lua http, server, location, location if
Access_by_lua http, server, location, location if
Content_by_lua location, location if
Header_filter_by_lua http, server, location, location if
Body_filter_by_lua http, server, location, location if
Log_by_lua http, server, location, location if
Init_by_lua:



When nginx the configuration file, run the Lua script inside, which is often used for global variable applications.
For example, an application that Lua_shared_dict shared memory is only emptied when the Nginx is reset, which is often used for statistics.



Set_by_lua:
Set a variable, commonly used to compute a logic, and then return the result
This phase cannot run the output API, control API, Subrequest API, Cosocket API



Rewrite_by_lua:
Run before the access stage, primarily for rewrite



Access_by_lua:
Mainly for access control, can collect most variables, similar to the status need in log phase.
This instruction runs at the end of the Nginx access phase, so it always runs after instructions such as allow and deny, although they belong to the access stage.



Content_by_lua:
The stage is the most important of all request processing stages, and the configuration instructions that run at this stage generally assume the content and output HTTP responses.



Header_filter_by_lua:
Typically only used to set cookies and headers, etc.
This phase cannot run the output API, control API, Subrequest API, Cosocket API



Body_filter_by_lua:
It is typically invoked multiple times in a single request because it is the so-called "streaming output" that implements the HTTP 1.1 chunked encoding.
This phase cannot run the output API, control API, Subrequest API, Cosocket API



Log_by_lua:



This phase always runs at the end of the request, for subsequent operations of the request, such as statistics in shared memory, and Body_filter_by_lua should be used if high accurate data statistics are to be applied.
This phase cannot run the output API, control API, Subrequest API, Cosocket API


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.