Click open link

one preface

We all know that Nginx has a lot of features and benefits, but developing on Nginx is a challenge. Nginx modules need to be developed in C, and must comply with a complex set of rules. Most importantly, developing modules in C must be familiar with Nginx source code, which makes it daunting for developers. For the sake of developers, we’ll introduce a framework that integrates Nginx and Lua, OpenResty, which helps us implement luA specification development, implement various businesses, and figure out the compilation order of each module. I think OpenResty is no stranger to many applications as system architectures are constantly upgraded and optimized.

\

two How OpenResty works

Nginx adopts the master-worker model. A master process manages multiple worker processes. The basic event processing is placed in Woker, and the master is responsible for some global initialization and worker management. In OpenResty, each Woker uses a LuaVM, and when a request is assigned to a Woker, a Coroutine is created in that LuaVM. Data isolation between coroutines, each coroutine has an independent global variable _G.

Ps. coroutines are similar to threads in multithreading: they have their own stack, their own local variables, and their own instruction Pointers, but share information such as global variables with other coroutines. The main difference between threads and coroutines is that, in the case of multiple processors, multithreading conceptually involves running multiple threads at the same time, whereas coroutines switch coroutines through code and only one coroutine is running at any one time. And the running coroutine will only be suspended if it is explicitly asked to be.

The schematic diagram is as follows:

\

3. The advantage of OpenResty

First, we chose to use OpenResty, which is composed of Nginx core plus many third-party modules. Its biggest highlight is the default integration of Lua development environment, so that Nginx can be used as a Web Server.

With Nginx’s event-driven model and non-blocking IO, high-performance Web applications can be implemented.

Furthermore, OpenResty provides a large number of components such as Mysql, Redis, Memcached, and so on, making it much easier to develop Web applications on Nginx. Currently, Nginx+Lua architecture is used in Jingdong real-time price, seconds kill, dynamic service, single product page, list page, etc., and other companies such as Taobao and Qunar.

\

Four. Introduction to Nginx and Lua

1. The Nginx:

(1) Advantages of Nginx

  • Lightweight Web services also use less memory and resources than Apache
  • Anti-concurrent Nginx processing requests asynchronously non-blocking whereas Apache blocking high concurrency nginx can maintain low resource consumption and high performance
  • Highly modular design writing modules is relatively easy
  • The community is active and the high performance modules are coming out fast

(2) Why Nginx has high performance and occupies less memory

Nginx is known for its high performance, and nginx’s high performance is inseparable from its architecture. Here, let’s take a quick and cursory look at the nginx architecture.

  • First of all, nginx uses the multi-process model. What are the benefits? First of all, for each worker process, an independent process does not need to lock, so the cost of locking is saved. Meanwhile, programming and problem finding are much more convenient. Secondly, independent processes can not affect each other. After one process exits, other processes are still working and the service will not be interrupted. The master process will soon start a new worker process. Of course, the abnormal exit of worker process must be a bug in the program. Abnormal exit will lead to the failure of all requests on the current worker, but it will not affect all requests, so the risk is reduced.
  • Nginx uses an asynchronous non-blocking approach to handle requests. What is asynchronous non-blocking? That’s what happens when a thread calls something like waiting IO, instead of blocking here, it does something else, waits for the IO to be ready, and then executes, which I’m not going to describe here.

2. lua:

(1) Lua is a small scripting language. The writer is Brazilian. The language is designed to be embedded in applications to provide flexible extension and customization capabilities for applications

(2) Features of Lua:

  • Lua scripts can be easily called by C/C++ code and can be called by C/C++ functions in reverse, making Lua widely used in applications. Not only as an extension script, but also as a common configuration file, replacing XML,Ini and other file formats, and easier to understand and maintain.
  • Written in standard C, Lua is simple and elegant, and can be compiled and run on almost any operating system or platform. A complete Lua interpreter is only 200K, making Lua the fastest of all scripting engines. All of this makes Lua the best choice as an embedded script.

\

Five. The installation of OpenResty

I have introduced OpenResty briefly, explained how it works, and introduced the advantages of Nginx and Lua to make it easier for you to understand. To give you an idea of the benefits of using this, I’ll take a quick look at installing OpenResty and building a simple limiting example to give you an idea of what it can do beyond the benefits mentioned above.

OpenResty installation:

(1) You need to install the required plug-ins in advance

1 yum  install  readline-devel pcre-devel openssl-devel

(2) Download ngx_OpenResty-1.7.7.7.2.tar. gz and unzip it

1 wget http: //openresty .org /download/ngx_openresty-1 7.7.2. tar .gz

(3) installation LuaJIT

123 cd  bundle /LuaJIT-2 .1-20150120/``make  clean &&  make  &&  make  install``ln  - sf, luajit - 2.1.0 - alpha /usr/local/bin/luajit

(4) Download the ngx_CACHE_purge module, which is used to clean the Nginx cache

1 wget https: //github .com /FRiCKLE/ngx_cache_purge/archive/2 . 3. tar .gz

(5) Download the nginx_upstream_check_module module used for ustream health check

1 wget https: //github .com /yaoweibin/nginx_upstream_check_module/archive/v0 3.0. tar .gz

Install ngx_openresty (6)

123456789 cd  /usr/servers/ngx_openresty-1 . 7.7.2 ` `. /configure --prefix= /usr/servers --with-http_realip_module  ``--with-pcre  ``--with-luajit ``--add-module=. /bundle/ngx_cache_purge-2 .3/ ``--add-module=. /bundle/nginx_upstream_check_module-0 3.0 / - j2 ` ` make  &&  make  install

The installation is successful if the following directory is displayed in the /usr/servers directory

(8) to start the nginx: / usr/servers/nginx/sbin/nginx (actually we can configure the good system environment variables, can be directly data configured nginx command)

\

Six. Anti-brush (black and white list) simple example of building

/usr/servers/nginx/conf/ create a simple lua file called example.lua.

Conf file, or we can cp nginx.conf example.conf or mkdir example.conf

2. Next we need to edit the nginx.conf startup configuration file

Conf file. Add the command include example.conf to the HTTP body of nginx.conf

Nginx. conf contains the following contents:

You can see the introduction of your own project file and the introduction of the Lua module in red.

3. Next we can write our logic implementation \ in example.conf

/usr/example/lua/ redis_black_limite. lua/ usr/example/lua/ redis_black_limite. lua The lua script is embedded in the Content phase and responds to the content. Here, let’s go inside and see how the corresponding code works:

This is a brushproof demo, from which we can see that I am more IP, value from redis, and then request IP match, to do the brushproof function, in addition, there is a statement, we notice, that is require, which acts as our class loader, Class. Forname to load lua modules in.

5. In addition, we also make a simple function to set blacklist and cancel blacklist, which can serve as our management center in the future. The specific logic is written in set_black.lua and cancel.lua, and then configure the URL in lua.conf, as shown in the following figure :\

Add the following code to the server body in lua.conf:

Next we need to write the set_black.lua and cancel.lua files, which are as follows:

123456789101112 v   set_black.lua:``local  redis =require  "resty.redis"``local  cache =redis.new()``cache:set_timeout(6000)``local  ok,err=cache.connect(cache, '192.168.150.61' ,6379)``if  not ok  then``         ngx.say( "failed to connect:" ,err)``         return``end``local  ip = ngx.var.remote_addr``cache: set ( "user:" ..ip.. ":block" ,1)``ngx.say( "user:" ..ip.. ":block" . "Setting blacklist succeeded" )
123456789101112 v   cancel_black.lua:``local  redis =require  "resty.redis"``local  cache =redis.new()``cache:set_timeout(6000)``local  ok,err=cache.connect(cache, '192.168.150.61' ,6379)``if  not ok  then``         ngx.say( "failed to connect:" ,err)``         return``end``local  ip = ngx.var.remote_addr``cache:expire( "user:" ..ip.. ":block" ,0)``ngx.say( "user:" ..ip.. ":block" . "Remove blacklist" )

\

Effect of 7.

After completing the above configuration, we can set the blacklist first, and then access the request. The return result is blacklisted, and then cancel the blacklist, and then access the request, and the access is successful. The specific results are shown in the following figure.

Seven. Conclusion \

The purpose of this article is to give you an overview of OpenResty and build a simple application. As the architecture is upgraded, we will slowly move some of the less complex business to the NGINx layer, thus improving our throughput and resolving some of the performance bottlenecks. For example, in the Nginx layer, simple traffic limiting, blacklist and whitelist, caching and other business complexity is not too strong, so as to increase our throughput rate, we can also filter out some garbage traffic in the Nginx layer, so that the Tomcat layer only needs to focus on business. (The above is my simple and rough understanding, if you have any questions, welcome to discuss)

\

Please reprint and mark: “This article is reprinted from linkedkeeper.com (article/Zeng Changrong)”

\