In this article, we'll learn more about Nginx/OpenResty. What the hell is Nginx

Posted by pradeepmamgain on Fri, 14 Jan 2022 15:42:13 +0100

Detailed explanation of Nginx/OpenResty

Nginx (or OpenResty) has been used in production scenarios to an amazing extent. No matter what the actual market occupation rate is, according to the projects experienced by the author in recent years, the utilization rate is 100%.

However, a large number of developers around the author still know the basic configuration of Nginx (or OpenResty), and do not know much about its core principles and high-performance configuration.

This article not only explains the core principles and high-performance configuration of Nginx, but also introduces the high concurrency practical programming of Nginx+Lua to help you master a new weapon to solve the high concurrency problem.

Introduction to Nginx

Nginx is a high-performance HTTP and reverse proxy Web server. It is rambler. Com, the second most visited Web server in Russia by Igor sesoyev Ru Web server for site development.

The source code of nginx is released in the form of BSD like license. Its first public version 0.1.0 was released on October 4, 2004 and version 1.0.4 was released on June 1, 2011. Nginx is famous all over the world for its high stability, rich feature set, low memory consumption and strong concurrency. At present, it is widely used. For example, Baidu, JD, Sina, Netease, Tencent and Taobao are users of nginx. The relevant addresses of nginx are as follows:

The source address is https://trac.nginx.org/nginx/browser. 
The official website address is http://www.nginx.org/. 

Nginx has three main community branches:

(1) Official version of Nginx: the update iteration is relatively fast, and free version and commercial version are provided.

(2) Tengine: Tengine is a Web server project initiated by Taobao. On the basis of Nginx, it adds many advanced functions and features for the needs of high-volume websites.

Tengine's performance and stability have been well tested on large websites (such as Taobao, tmall mall, etc.). Its ultimate goal is to build an efficient, stable, secure and easy-to-use Web platform.

(3) OpenResty: in 2011, Chinese teacher Zhang Yichun embedded LuaJIT VM into Nginx and realized OpenResty, a high-performance server-side solution. OpenResty is a high-performance Web platform based on Nginx and Lua. It integrates a large number of sophisticated Lua libraries, third-party modules and most dependencies. It is used to easily build dynamic Web applications, Web services and dynamic gateways that can handle ultra-high concurrency and high scalability. OpenResty aims to make Web services run directly inside Nginx services, make full use of Nginx's non blocking I/O model, and make consistent high-performance responses not only to HTTP client requests, but also to remote backend (such as MySQL, PostgreSQL, Memcached and Redis).

OpenResty effectively turns Nginx into a powerful general Web application platform by bringing together various well-designed Nginx modules (mainly independently developed by OpenResty team), so that Web developers and system engineers can use Lua script language to mobilize various C and Lua modules supported by Nginx, Quickly construct a high-performance Web application system capable of single machine concurrent connection of 10KB or more than 1000KB.

You can view the components supported by OpenResty through the following link address on the OpenResty official website:

The official website address is https://openresty.org/cn/. 
The component address is https://openresty.org/cn/components.html. 

Forward proxy and reverse proxy

Here is a brief introduction to forward proxy and reverse proxy. Both forward proxy and reverse proxy are used to forward client requests in proxy services, but there is still a big difference.

The biggest feature of forward proxy is that the client is very clear about the server address to be accessed, as shown in Figure 7-1.

Figure 7-1 characteristics of forward agent

In the forward proxy server, the client needs to configure the target server information, such as IP and port. Generally speaking, the forward proxy server is a LAN internal machine connected to the client network, or a dual network card machine that can connect two isolated networks. Through forward proxy, the HTTP request of the client can be forwarded to other different target servers that are not connected to the client network.

Reverse proxy is opposite to forward proxy. The client does not know the information of the target server. The proxy server is like the original target server. The client does not need to make any special settings.

The biggest feature of reverse proxy is that the client does not know the address of the target server, as shown in Figure 7-2.

Figure 7-2 characteristics of reverse proxy

The client directly sends the request to the reverse proxy server, and then the reverse proxy forwards the request to the target server, and returns the response result of the target server to the client according to the original path.

Usage scenario description of forward proxy and reverse proxy:

(1) The main scenario of forward proxy is the client. Due to physical reasons such as network failure, it is necessary to smoothly access the target server through the intermediate forwarding link of forward proxy server. Of course, you can also disguise and change some details of the client through the forward proxy server.

(2) The main scenario of reverse proxy is the server. The service provider can easily realize the dynamic switching of target servers and load balancing of multi-target servers through the reverse proxy server.

Generally speaking, forward Proxy (such as Squid and Proxy) is a disguise of the client, hiding the client's IP, header or other information, and the server gets the disguised client information; Reverse Proxy (such as Nginx) is a camouflage of the target server, hiding the IP, header or other information of the target server, and the client gets the camouflaged target server information.

Detailed explanation of startup commands and parameters of Nginx

After installing OpenResty on the Windows platform and setting the path environment variable, you can start OpenResty. The original startup command of OpenResty is nginx, and its parameters are - v, - t, - p, - c, - s, etc. the instructions are as follows:

(1) - v: indicates to view the version of Nginx.

C:\dev\refer\LuaDemoProject\src> nginx -v
nginx version: openresty/1.13.6.2

(2) - c: specify a new Nginx configuration file to replace the default Nginx configuration file.

//At startup, switch to the src directory in the cmd window, and then execute the following command
C:\dev\refer\LuaDemoProject\src> nginx -p ./ -c nginx-debug.conf

(3) - t: represents the configuration file for testing Nginx. If you are not sure whether the syntax of the Nginx configuration file is correct, you can test it through the - t parameter of the Nginx command. This parameter means that the configuration file is not run, but only tested.

C:\dev\refer\LuaDemoProject\src> nginx -t -c nginx-debug.conf
nginx: the configuration file ./nginx-debug.conf syntax is ok
nginx: configuration file ./nginx-debug.conf test is successful

(4) - P: indicates to set the prefix path. C:\dev\refer\LuaDemoProject\src> nginx -p ./ - c nginx-debug. Conf in the above command, "- P. /" means that the current directory C:

\dev\refer\LuaDemoProject\src As a prefix path, that is, nginx
debug.conf The relative paths used in the configuration file are prefixed with this prefix.

(5) - s: indicates sending signals to the Nginx process, including stop and reload.

//Restart the Nginx process and send the reload signal
C:\dev\refer\LuaDemoProject\src> nginx -p ./ -c nginx-debug.conf -s reload
//Stop the Nginx process and send a stop signal
C:\dev\refer\LuaDemoProject\src> nginx -p ./ -c nginx-debug.conf -s stop

Start and stop scripts of OpenResty under Linux

Why introduce the start and stop scripts of OpenResty under Linux system?

(1) There are no useful start and stop scripts in the Nginx/OpenResty distribution package.

(2) Mastering some basic script instructions and being able to write basic running scripts is a necessary basic ability for Java engineers. The interview question "which Linux operation instructions have you used" will appear in many interview scenarios.

As a reference, here is a startup script of OpenResty/Nginx under Linux commonly used by the author, which is published on the online disk of crazy maker circle community. Its contents are as follows:

#!/bin/bash
#Set the installation directory of OpenResty
OPENRESTRY_PATH="/usr/local/openresty"
#Set the working directory of the Nginx project
PROJECT_PATH="/work/develop/LuaDemoProject/src/"
#Set the profile for the project
#PROJECT_CONF="nginx-location-demo.conf"
PROJECT_CONF="nginx.conf"
echo "OPENRESTRY_PATH:$OPENRESTRY_PATH"
echo "PROJECT_PATH:$PROJECT_PATH"
#Find all process IDs of Nginx
pid=$(ps -ef | grep -v 'grep' | egrep nginx| awk '{printf $2 " "}')
#echo "$pid"
if [ "$pid" != "" ]; then
 #If it is already executing, you will be prompted
 echo "openrestry/nginx is started already, and pid is $pid, operating failed!"
else
 #If not, start
 $OPENRESTRY_PATH/nginx/sbin/nginx -p ${PROJECT_PATH} \
 -c ${PROJECT_PATH}/conf/${PROJECT_CONF}
 pid=$(ps -ef | grep -v 'grep' | egrep nginx| awk '{printf $2 " "}')
 echo "openrestry/nginx starting succeeded!"
 echo "pid is $pid "

fi before using the above script, you need to configure three options in the script: the installation directory of OpenResty/Nginx, the working directory of the project and the configuration file of the project. After the configuration is completed, execute OpenResty start. Exe in the Linux command window SH start the script to start OpenResty.

[root@localhostlinux]#/work/develop/LuaDemoProject/sh/linux
/openresty-start.sh
OPENRESTRY_PATH:/usr/local/openresty
PROJECT_PATH:/work/develop/LuaDemoProject/src/
openrestry/nginx starting succeeded!
pid is 31403 31409

The following is a brief introduction to openresty - start Instructions mainly used in SH script:

(1) echo display command: used to display information to the terminal screen.

(2) ps process list: used to display the list of processes currently running on the local machine.

(3) grep find command: used to find the qualified strings in the file.

The above three commands are frequently used and very basic Linux commands. In addition to the above OpenResty start In addition to the SH script, three other useful OpenResty operation scripts are provided, as follows:

(1)openresty-stop.sh is used to stop OpenResty/Nginx.

(2)openresty-status.sh is used to output the running status and process information of OpenResty/Nginx.

(3)openresty-restart.sh is used to restart OpenResty/Nginx.

Start and stop scripts of OpenResty under Windows

In addition to providing Shell scripts under Linux, Windows script files are also provided here. Scripts under Windows are usually called batch scripts, and the extension of batch scripts is bat, which contains a series of DOS commands.

As a reference, here is a script for starting, stopping, restarting and viewing the status of OpenResty/Nginx under Windows. You can download it on the online disk of crazy maker circle community, including the startup script openresty start The specific contents of bat are as follows:

@echo off
rem Start flag flag=0 Indicates that it has been started before, flag=1 Means start now
set flag=0
rem set up OpenResty/Nginx Installation directory for
set installPath=E:/tool/openresty-1.13.6.2-win32
rem set up Nginx Working directory of the project
set projectPath=C:/dev/refer/LuaDemoProject/src
rem Set the profile for the project
set PROJECT_CONF=nginx-location-demo.conf
rem set PROJECT_CONF=nginx.conf
echo installPath: %installPath%
echo project prefix path: %projectPath%
echo config file: %projectPath%/conf/%PROJECT_CONF%
echo openresty starting...
rem lookup OpenResty/Nginx Process information, and then set flag Flag bit
tasklist|find /i "nginx.exe" > nul
if %errorlevel%==0 (
echo "OpenResty/Nginx already running ! "
rem exit /b
) else set flag=1
rem Start if necessary OpenResty/Nginx
cd /d %installPath%
if %flag%==1 (
start nginx.exe -p "%projectPath%" -c "%projectPath%/conf/%PROJECT_CONF%"
ping localhost -n 2 > nul
)

rem outputs the process information of OpenResty/Nginx

tasklist /fi "imagename eq nginx.exe"
tasklist|find /i "nginx.exe" > nul
if %errorlevel%==0 (
echo "OpenResty/Nginx starting succeeded!"
)

Before using, start the script OpenResty - start After configuring the three options of OpenResty/Nginx installation directory, project working directory and project configuration file in bat, execute OpenResty start in the Windows CMD command window Bat start script to start OpenResty.

PS C:\dev\refer\LuaDemoProject\sh\windows> .\openresty-start.bat
installPath: E:/tool/openresty-1.13.6.2-win32
project prefix path: C:/dev/refer/LuaDemoProject/src
config file: C:/dev/refer/LuaDemoProject/src/conf/nginx-location-demo.conf
openresty starting...
"OpenResty/Nginx already running ! "
Image Name  PID Session name session# Memory usage
========================= ======== ================ =========== ============
nginx.exe 34264 Console 2 9,084 K
nginx.exe 25912 Console 2 8,992 K
"OpenResty/Nginx starting succeeded!"

above. The main instructions used in the bat batch file are briefly introduced as follows:

(1) rem annotation command: it is generally used to annotate the program. The content after the command is not executed.

(2) echo display command: used to display information to the terminal screen.

(3) cd directory switching: used to switch the current directory.

(4) tasklist process list: used to display the list of processes currently running on the local or remote machine.

Except for OpenResty - start. Above Bat script, for Windows system, three useful OpenResty operation batch scripts are also provided in the supporting source code of this paper, as follows:

(1)openresty-stop.bat: used to stop OpenResty/Nginx.

(2)openresty-status.bat: used to output the running status and process information of OpenResty/Nginx.

(3)openresty-restart.bat: used to restart OpenResty/Nginx.

These scripts are very useful in terms of efficiency. You can download, study and customize from the crazy maker circle community online disk.

The content of this article is the detailed explanation of Nginx/OpenResty and the introduction of Nginx

  1. The next article explains the detailed explanation of Nginx/OpenResty and the core principle of Nginx;
  2. Friends who think the article is good can forward this article and pay attention to the Xiaobian;
  3. Thank you for your support!

Topics: Java Programming Nginx Interview Programmer