Introduction and installation of Nginx (Nginx learning one)

1. Introduction to Nginx

(1) Speaking of Nginx, perhaps the first thing you think of is its load balancing and reverse proxy functions. That's right, these are the two most frequently used functions of Nginx, but Nginx has more than just these two functions, and its role is quite big.
(2) Nginx was designed and developed by Russian Igor Sysoev. The development work began in 2002 and the first public release was on October 4, 2004.

The official website is: http://nginx.org/. It is a free and open source high-performance HTTP proxy server and reverse proxy server (Reverse Proxy) product, and it can also provide IMAP/POP3 mail proxy services and other functions. Its high concurrency performance is very good, the official test can support 50,000 concurrency; the runtime memory and CPU usage are low, the configuration is simple, easy to use, and the operation is very stable.

2. Common functions of Nginx

In fact, Nginx has many functions. Here I only introduce a few commonly used functions. For details, please refer to the official website.
  (1) Reverse proxy
  This is one of the main functions of the Nginx server as a WEB server. When a client sends a request to the server, it will first pass through the Nginx server, and the server will distribute the request to the corresponding WEB server. The forward proxy is the proxy client, while the reverse proxy is the proxy server. Nginx uses regular expressions for related configuration and adopts different forwarding strategies in providing reverse proxy services. The configuration is quite flexible, and the backend is configured When forwarding a request, you don't need to care about the network environment at all. You can specify any IP address and port number, or other types of connections, requests, etc.
  (2) Load balancing
  is also one of the most commonly used functions of Nginx. On the one hand, load balancing is to share a single heavy load on multiple network nodes for parallel processing. After each node is processed, the results are summarized and returned to the user. This can greatly improve the processing capacity of the network system; on the other hand, a large number of front-end concurrent requests or data traffic are shared to multiple back-end network nodes for separate processing, which can effectively reduce the waiting time of front-end users. Nginx load balancing belongs to the latter aspect, mainly to shunt a large amount of front-end access or traffic, which has ensured the efficiency of front-end user access and can reduce the processing pressure of the back-end server.
  (3) Web caching
  In many excellent websites, Nginx can be used as a front-end caching server. It is used to cache front-end requests, thereby improving the performance of the web server. Nginx will create a local copy of the content that the user has accessed on the server, so that if the data is accessed again within a period of time, there is no need to make a request to the backend through the Nginx server. Reduce network congestion, reduce data transmission delay, and increase user access speed.

3. Nginx installation and initial use

Regarding the installation of Nginx, it is divided into installation on the Windows platform and Linux platform. The Windows version of the Nginx server is less efficient than the Linux version of the Nginx server, and the actual use is generally the Linux platform Nginx server. Therefore, we will focus on the Linux version when we introduce it later.
(1) Download address
Nginx download address: http://nginx.org/en/download.html The
Insert picture description here
development version is mainly used for the research and development of Nginx software projects. The stable version indicates that it can be used as a Web server for commercial applications. Here we choose the current stable version: nginx-1.18.0
(2) Windows version installation
We will unzip the downloaded nginx-1.18.0.zip file.
After decompression, as shown in the figure:
Insert picture description here
Introduction to the main folders in this directory:

1. conf directory: stores the main configuration files of Nginx. Many functions are realized by configuring the nginx.conf file in this directory, which we will introduce in detail later.

2. The docs directory: stores the main documentation of the Nginx server, including LICENSE of the Nginx server, LICENSE of OpenSSL, LICENSE of PCRE, and LICENSE of zlib, as well as the version change instructions for the Nginx server upgrade of this version, and the README file.

3. html directory: Two static webpage files with the suffix .html are stored. These two files are related to the operation of the Nginx server.

4. Logs directory: store log files of Nginx server operation.

5. nginx.exe: Start the exe file of the Nginx server. If the nginx.conf file in the conf directory is configured correctly, the Nginx server can be started through this file.
  1. Start Nginx
  Double-click the nginx.exe file in the directory after decompression, a flashing screen appears, and the startup is successful.

Enter http://localhost or http://localhost:80 in the browser. The following interface appears, indicating successful startup.
  Insert picture description here
  ps: This page is the index.html file in the html directory in the unzipped directory above.
  Two, close nginx

Open the command line, switch to the unzipped directory, and enter the following command:

nginx.exe -s stop

Or open the task manager, find the nginx process, and right-click to end the process.
Three, nginx commonly used commands under windows

cmd 进入Nginx解压目录 执行以下命令

启动nginx服务: start nginx 

修改配置后重新加载生效: nginx -s reload

重新打开日志文件: nginx -s reopen

测试nginx配置文件是否正确: nginx -t -c /path/to/nginx.conf 

验证配置是否正确: nginx -t

查看Nginx的版本号: nginx -V

启动Nginx: start nginx

快速停止或关闭Nginx: nginx -s stop

正常停止或关闭Nginx: nginx -s quit

配置文件修改重装载命令: nginx -s reload

清除ngnix所有进程: taskkill /f /t /im nginx.exe

(3) Linux version installation The
selected Linux system is CentOS7.3.
One, install nginx environment

1 yum install gcc-c++
2 yum install -y pcre pcre-devel
3 yum install -y zlib zlib-devel
4 yum install -y openssl openssl-devel

For gcc, to install nginx, you need to compile the source code downloaded from the official website first, and the compilation depends on the gcc environment. If there is no gcc environment, you need to install gcc.

For pcre, prce (Perl Compatible Regular Expressions) is a Perl library, including a Perl compatible regular expression library. The http module of nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux.

For zlib, the zlib library provides a variety of compression and decompression methods. Nginx uses zlib to gzip the content of the http package, so you need to install the zlib library on Linux.

For openssl, OpenSSL is a powerful secure socket layer cryptographic library, including the main cryptographic algorithms, commonly used key and certificate packaging management functions, and SSL protocol, and provides a wealth of applications for testing or other purposes. Nginx not only supports the http protocol, but also supports https (that is, HTTP is transmitted over the ssl protocol), so you need to install the openssl library in Linux.
Two, compile and install

First copy the downloaded nginx-1.18.0.tar.gz file to the Linux system, and then decompress it:

1 tar -zxvf nginx-1.18.0.tar.gz

Then enter the decompressed directory, compile and install.

1 cd nginx-1.18.0
2 ./configure --prefix=/usr/local/nginx
3 make
4 make install

Note: Specify /usr/local/nginx as the directory where nginx service is installed
  .
Three, start nginx

Enter the /usr/local/nginx directory, the file directory is displayed as follows:

1 cd /usr/local/nginx

Insert picture description here

Then we enter the sbin directory and start nginx:

1 cd sbin
2 ./nginx

Of course, environment commands can also be configured so that nginx can be started in any directory.

Linux is good news if there is no news, and no information is prompted to indicate a successful startup.
 
Or you can enter the following command to check whether nginx services are running:

1 ps -ef | grep nginx

Then we enter the IP address of the Linux system in the browser, and a successful window installation interface appears.
Insert picture description here

Fourth, close nginx

There are two ways:

Method 1: Quick stop

1 cd /usr/local/nginx/sbin
2 ./nginx -s stop

This method is equivalent to first finding out the nginx process id and then using the kill command to forcefully kill the process. Not very friendly.

Method 2: Stop gently

1 cd /usr/local/nginx/sbin
2 ./nginx -s quit

This method means that the nginx service is allowed to complete the processing of the network request currently being processed, but does not receive a new request, then close the connection and stop working.

Five, restart nginx

Method 1: Stop and then start

1 ./nginx -s quit
2 ./nginx

It is equivalent to executing the stop command first and then executing the start command.

Method 2: Reload the configuration file

1 ./nginx -s reload

Usually we use nginx to modify the configuration file nginx.conf the most. After the modification, you can use this command if you want the configuration file to take effect without restarting nginx.
Six, configure nginx command
1. Edit the /etc/profile file

1 vi /etc/profile

2: Add configuration as follows
Insert picture description here
configuration content

PATH=$PATH:/usr/local/nginx/sbin
export PATH

ps: /usr/local/nginx/sbin is the sbin directory under the nginx installation path

Effective configuration:

1 source /etc/profile

Then let's take a look at the effect

1 nginx -v

You can see the nginx version information
Insert picture description here

Seven, check whether the configuration file syntax is correct

Method 1: Specify the configuration file to be checked through the following command

1 nginx -t -c  /usr/local/nginx/conf/nginx.conf

The grammatically correct result is as follows:Insert picture description here

Method 2: Use the following command without the -c parameter to check the nginx.conf configuration file by default.

1 nginx -t 

The grammatically correct result is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42301302/article/details/106135265