Openresty case explanation

Keywords: Operation & Maintenance Nginx lua


We all know that nginx has many features and benefits, but it has become a difficult problem to develop on nginx. Nginx modules need 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. For the convenience of developers, next we will introduce a framework that integrates nginx and lua, OpenResty, which helps us realize the development with lua's specification, realize various businesses, and help us figure out the compilation order of each module. As for OpenResty, I think you should no longer be unfamiliar. With the continuous upgrading and optimization of system architecture, OpenResty is widely used in.

Introduction to OpenResty

1. OpenResty is also known as ngx_openresty is the core Web application server based on Nginx
2. OpenResty is a high-performance Web platform based on Nginx and Lua. OpenResty effectively turns Nginx into a powerful general Web application platform by aggregating various well-designed Nginx modules
3. The goal of OpenResty is to make Web services run directly inside the Nginx service, make full use of the non blocking I/O model of Nginx, and make a series of high-performance responses not only to HTTP client requests, but also to remote backend DB
4. OpenResty relies on Nginx's event driven model and non blocking IO to achieve high-performance Web applications
5. OpenResty enables us to use Lua to access back-end DB and other services asynchronously and concurrently with the help of Nginx's asynchronous and non blocking
6. OpenRest uses ngx.location.capture_multi greatly reduces the number of HTTP connections of the browser and can access the background interface asynchronously and concurrently

How OpenResty works

Nginx adopts the master worker model, that is, a master process manages multiple worker processes. The basic time processing is placed in the worker process. The master process is responsible for global initialization and worker management

In OpenResty, each worker process uses a LuaVM. When a request is assigned to a worker, a coroutine collaboration will be created in the LuaVM. The data between the collaborations is isolated, and each collaboration has independent global variables

A simple explanation of process, thread and co process

Relationship between process and thread

1. We all know that the core of computer is CPU, which undertakes the tasks of all computers. It is like a factory running all the time
2. Assuming that the power of the factory is limited and can only be supplied to one workshop at a time, that is, when one workshop starts, other workshops must stop working. The implication behind this is that a single CPU can only perform one task at a time
3. A process is like a workshop in a factory. It represents a single task that the CPU can handle. At any time, the CPU always runs one process and other processes are not running
4. There can be many workers in a workshop. They worked together to complete a task
5. Threads are like workers in a workshop. A process can contain multiple threads
6. The workshop space is shared by workers. For example, many rooms are accessible to every worker, which indicates that the memory space of a process is shared and can be used by each thread
7. However, the size of each room is different. Some rooms can only accommodate one person at most. For example, when there are people in the toilet, others can't enter. This means that when a thread uses some shared memory, other threads must wait for him to finish before they can use this piece of memory
8. A simple way to prevent others from entering is to add a lock at the door. People who arrive first lock the door, and those who arrive later see the lock, line up at the door and wait until the lock is opened. This is called "mutually exclusive lock", which prevents multiple threads from reading and writing a memory area at the same time
9. There are also some rooms that can accommodate n people at the same time, such as the kitchen. In other words, if the number of people is greater than N, the extra can only wait outside. This is like some memory areas, which can only be used by a fixed number of threads
10. At this time, the solution is to hang n keys at the door. Those who enter take one key and hang it back when they come out. People who arrive later find that the key is overhead and know that they must wait at the door. This signal is called "semaphore" to ensure that multiple threads will not conflict

  Co process description  

1. A coroutine is neither a process nor a thread. It is just a function, a special function -- it can be suspended somewhere and can continue to execute again. Compared with process, process and thread, it is not a dimensional concept. ngx_cache_purge-2.3.tar.gz
2. A process can contain multiple threads, and a thread can contain multiple coprocedures. However, the operation of multiple coprocedures in a thread is serial. That is, when one process runs, other processes must be suspended
3. Coprocesses are not suitable for computing intensive scenarios. Coprocesses are suitable for I/O (input / output, read / write) blocking

Advantages of OpenResty

1. First, we choose to use OpenResty, which is composed of Nginx core and many third-party modules. Its biggest highlight is that it integrates Lua development environment by default, so that Nginx can be used as a Web Server.

2. With the help of Nginx's event driven model and non blocking IO, high-performance Web applications can be realized.

3. Moreover, OpenResty provides a large number of components, such as Mysql, Redis, Memcached, etc., which makes it easier and easier to develop Web applications on Nginx. At present, the Nginx+Lua architecture is used in JD, such as real-time price, second kill, dynamic service, single product page, list page, etc. other companies, such as Taobao, qunar, etc.

Installation of Openresty

Download: OpenResty - Download

First install openresty

[root@C7--11 ~]# yum -y install wget          
[root@C7--11 ~]# wget -O /etc/yum.repos.d/CentOS7-Base-163.repo                        
Saving to: "/etc/yum.repos.d/CentOS7-Base-163.repo"

100%[=========================================================================================>] 1,572       --.-K/s Time 0 s      

2021-12-04 03:20:32 (300 MB/s) - Saved“/etc/yum.repos.d/CentOS7-Base-163.repo" [1572/1572])

[root@C7--11 ~]# yum -y install epel-release                              

[root@C7--11 ~]# tar -zxvf openresty- -C /usr/src                             

[root@C7--11 ~]# yum install pcre-devel openssl-devel gcc curl                  

#This module is used to clean up the cache
[root@C7--11 ~]# wget                                       
#This module is used for ustream health check
[root@C7--11 ~]# wget                                                

[root@C7--11 ~]# tar xf ngx_cache_purge-2.3.tar.gz -C /usr/src/openresty-                                                                        
[root@C7--11 ~]# tar xf nginx_upstream_check_module-0.3.0.tar.gz -C /usr/src/openresty-                          
[root@C7--11 ~]# cd /usr/src/openresty-                         

[root@C7--11 openresty-]# ./configure --prefix=/usr/local/openresty --with-http_realip_module --with-http_ssl_module  --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/                               


--with***                      #Install some built-in / integrated modules
--with-http_realip_module      #Take the user's real ip module
-with-pcre                     #Perl compatible expression module
--with-luajit                  #Integrated luajit module
--add-module                   #Add a customized third-party module, such as ngx_che_purge

[root@C7--11 openresty-]# gmake && gmake install               
[root@C7--11 openresty-]# ln -s /usr/local/openresty/nginx /usr/local/nginx            
[root@C7--11 openresty-]# vim /etc/profile                
...         Last add content
export ORPATH=/usr/local/openresty
export PATH=$PATH:$ORPATH/bin:$ORPATH/nginx/sbin


[root@C7--11 openresty-]# useradd -r -s /sbin/nologin -g www www             
[root@C7--11 openresty-]# vim /usr/local/openresty/nginx/conf/nginx.conf

user  www www;     #Revised to www


                                          Copy and paste directly
[root@C7--11 openresty-]# cat >> /usr/lib/systemd/system/nginx.service  << EOF
Description=nginx - high performance web server

ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit


[root@C7--11 openresty-]# systemctl start nginx

Access test

  Configure nginx.conf file

[root@C7--11 ~]# vi /usr/local/openresty/nginx/conf/nginx.conf
...........     stay http Add content to
http {
    include       mime.types;
    default_type  application/octet-stream;

#lua module path, between multiple ";" Separation, where ';;' Indicates the default search path. The default search path is / usr/local/openresty /

    lua_package_path "/usr/local/openresty/lualib/?.lua;;";    #lua module
    lua_package_cpath "/usr/local/openresty/lualib/?.so;;";    #c module
    include lua.conf;


  Create lua.conf file

[root@C7--11 ~]# vi /usr/local/openresty/nginx/conf/lua.conf    #Add the following

server {
        listen       8080;                  
        server_name  _;

    location /lua {
    default_type 'text/html';
        content_by_lua 'ngx.say("hello world")';

   location /filelua {
        default_type 'text/html';
        lua_code_cache off;                     #Close the cache so that reload nginx is not required for each modification of lua code during debugging
        content_by_lua_file conf/lua/test.lua;
[root@C7--11 ~]# mkdir /usr/local/openresty/nginx/conf/lua
[root@C7--11 ~]# cd /usr/local/openresty/nginx/conf/lua
[root@C7--11 lua]# vi test.lua
ngx.say("hello world,bbb file");


[root@C7--11 ~]# systemctl restart nginx

Posted by Bauer418 on Fri, 03 Dec 2021 13:26:34 -0800