In order to handle a large number of connection requests, non-blocking I/O and multiplexing are required. Select, poll, and epoll are the I/O multiplexing methods provided by the Linux API. Since the addition of epoll in Linux2.6, the high-performance server space has been widely used. Nignx uses epoll to implement I/O multiplexing and support high concurrency.

For “high performance” servers, the focus is not on language performance, but caching and language support for asynchronous non-blocking.

For caches, you need to understand the order of communication speed:

  • Memory >SSD> Mechanical Disks
  • Local > Network
  • The goal of the in-process > inter-process cache system is to achieve the highest in-process hit ratio and thus the highest overall efficiency of the cache system.

Asynchronous non-blocking if you want to access a database, network, or slower IO devices, don’t spend a lot of time waiting. Instead, it uses an event-driven approach, notifying us when the system has completed a task. This allows free server CPU resources to be used to service client connections.

OpenResty OpenResty is a high-performance Web platform based on Ngnix and Lua, with excellent internal integration of Lua libraries, third-party modules and dependencies. It is used to build dynamic Web applications, Web services and dynamic gateways that can handle high concurrency and high scalability. You can use the Lua script to call THE C and Lua modules supported by Ngnix, and quickly build a high-performance Web application system with concurrent connection between 10K and 1000K. The goal of OpenResty is to have Web services running directly inside Nginx services, leveraging the non-blocking IO model of Ngnix to provide consistent, high-performance responses to HTTP client requests and back-end DB.

OpenResty has revolutionized the development model of high-performance servers. OpenResty is actually the perfect combination of Nginx and LuaJIT.

Because Nginx adopts the master-worker model, that is, a master process manages multiple worker processes, and the basic event processing is placed in the worker, while the master is only responsible for some of the whole play initialization and worker management. In OpenResty, each worker uses a LuaVM, and each request assigned to the worker creates a Coroutine in this LuaVM. Data isolation between coroutines, each coroutine has an independent global variable _G.

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

Based on actual tests, OpenResty performance is close to or even better than the C Module, the king of Nginx performance.

OpenResty architecture

Load balancing

LVS+HAProxy forwards traffic to core Nginx1 and Nginx2, which achieves load balancing of traffic.

Single closed-loop

All desired data can be obtained directly from this server, most of the time without having to go over the network or go to another server to obtain.

Distributed closed-loop Single machine closed-loop encounters two major problems

Data inconsistency

For example, there is no master/slave architecture, causing data inconsistency between different servers

Storage Bottleneck

The disk or memory hit the ceiling

The best way to solve the data inconsistency is to use the master slave or distributed centralized storage. However, when the storage bottleneck is encountered, the data needs to be fragmented according to the service key and distributed to multiple servers.

Access gateway

The access gateway is also called the access layer, which is the entrance to receive traffic. At the entrance, it does the following:

Perl, libpcre, and libssl libraries must be installed before the OpenResty environment installation.

$sudo apt install libpcre3-dev libssl-dev perl make build-essential curl $sudo apt install libpcre3-dev libssl-dev perl make build-essential curl  libreadline-dev libncurses5-devCopy the code

Download and unzip OpenResty to its directory

$$tar wget https://openresty.org/download/ngx_openresty-1.13.6.1.tar.gz - ZXVF ngx_openresty - 1.13.6.1. Tar. Gz $mv Openresty -1.13.6.1 openResty $CD OpenResty $./configureCopy the code

The default installation directory is /usr/local/openresty

Compile and install

$ sudo make && make install
$ cd /usr/local/openresty
Copy the code

Start the Nginx

$ sudo /usr/local/openresty/nginx/sbin/nginx
$ ps -ef | grep nginx
$ service nginx status
Copy the code

Nginx startup if present

Nginx: [emerg] Bind () to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] Still could not bind()Copy the code

Note Port 80 is occupied. Check whether port 80 is occupied and restart the port. The reason is that nginx listens on ipv4 port 80 and then on ipv6 port 80.

$ sudo netstat -ntlp | grep 80
$ sudo killall -9 nginx
Copy the code

Re-edit the Nginx configuration file

$ sudo vim /etc/nginx/conf/nginx.conf

listen 80;
listen [::]:80 ipv6only=on default_server;
Copy the code

Use the curl tool or access the default port 80 in your browser

$ curl 127.0.0.1

浏览器输入http://127.0.0.1/

Configure the Nginx tool into the system environment variables for the current user

$ sudo vim ~/.bashrc
export PATH=$PATH:/usr/local/openresty/nginx/sbin
$ source ~./bashrc
$ cd ~
$ nginx -s reload
nginx: [alert] kill(12267, 1) failed (1: Operation not permitted)
Copy the code

Develop document www.nginx.com/resources/w… Ubuntu installs the Vcode or Sublime Text editor

content_by_lua $ vim /usr/local/openresty/nginx/conf/nginx.conf location /test { default_type text/html; content_by_lua 'ngx.say("hello openresty")'; } # restart Nginx $/ usr/local/openresty/Nginx/sbin/Nginx - # s reload browser to access 127.0.0.1 / test content_by_lua_file $vim nginx.conf location /test { content_by_lua_file 'html/test.lua'; } $ vim .. / HTML/test. The lua NGX. Say, "hello lua") $sudo/usr/local/nginx/sbin/reload nginx - s $curl 127.0.0.1 / test hello luaCopy the code

To avoid the need to restart Nginx for each change, configure the lua_code_cache option in the server option of Nginx.

$ vim nginx.conf
server{
  lua_code_cache off;
  location /test{
    content_by_lua_file 'html/test.lua';
  }
}
$ sudo /usr/local/openresty/nginx/sbin/nginx -s reload
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:48
Copy the code

OpenResty start creates a working directory

After OpenResty is installed, there are configuration files and associated directories. To ensure that the working directory and installation directory do not interfere with each other, create an OpenResty working directory and write the configuration separately.

$mkdir -p ~ / openresty/test/logs ~ / openresty/test/conf $vim ~ / openresty/test/conf/nginx. Conf # set nginx worker number work process, Number of CPU cores. worker_processes 1; Error_log logs/error.log; # set the maximum number of connections for each worker_connections 10224; } HTTP {# host block definition server{# listen port listen 8001; Location /{default_type text/ HTML; content_by_lua_block{ ngx.say("hello world"); }}}} $nginx -p ~/openresty/test $curl 127.0.0.1:8001 Hello world $vim nginx.conf location /test{ content_by_lua_file "lua/test.lua"; } $ cd .. && mkdir lua && cd lua $ vim test.lua local args = ngx.req.get_uri_args() local salt = args.salt if not salt then ngx.exit(ngx.HTTP_BAD_REQUEST) end local md5str = ngx.md5(ngx.time().. Salt) NGX. Say (md5str) $sudo/usr/local/openresty reload/nginx/sbin/nginx - s $curl -i 127.0.0.1 / test? Salt =lua HTTP/1.1 200 OK Server: openResty /1.13.6.2 Date: Sun, 27 Jan 2019 10:07:17 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive b55b77f75e46b96b11778ca7edfe8d55Copy the code

If an error occurs in the code, you need to directly view the Nginx error log

$ vim nginx/logs/error.log
2019/01/27 17:37:15 [error] 15764#0: *6 failed to load external Lua file "/usr/local/openresty/nginx/test.lua": 
cannot open /usr/local/openresty/nginx/test.lua: No such file or...
Copy the code

Check the Nginx process in Windows

Lambda tasklist/fi "imagename eq nginx. Exe" image name PID session name session # memory use = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =========== ============ nginx.exe 9072 Console 1 7,840 K nginx.exe 7692 Console 1 12 304 K nginx.exe 8120 Console 1 Exe 4552 Console 1 12 188 K nginx.exe 9588 Console 1 7 828 K nginx.exe 6256 Console 1 12 216 K nginx.exe 7308 Console 1 7 828 K nginx.exe 10192 Console 1 12 212 K λ taskkill /im nginx.exe /fCopy the code

Success: process “nginx.exe” with PID 9072 was terminated.