When it comes to Nginx, the first thing you can think of is its load balancing and the function of the reverse proxy. Yes, this is the most frequent use of Nginx two features, but Nginx is not just the two features, its role is quite large, this series of blog to slowly solve the mysterious veil of Nginx.
1, Nginx's introduction
Nginx was designed and developed by the Russian Igor Sysoev, and the development work began in 2002, the first public release on October 4, 2004.
The official website is: http://nginx.org/. It is a free open source high-performance HTTP proxy server and reverse proxy server (Reverse proxy) products, and it can also provide IMAP/POP3 mail agent service and other functions. Its high concurrency performance is good, the official test can support 50,000 of the concurrency, runtime memory and CPU usage is low, configuration is simple, easy to use, and run very stable.
2, Nginx's common functions
In fact, Nginx features particularly many, here I only introduce a few common functions, specific people can refer to the official website introduction.
①, Reverse Proxy
This is the Nginx server as one of the main functions of the WEB server, when the client sends a request to the server, it passes through the Nginx server, and the server distributes the request to the appropriate Web server. The forward proxy is a proxy client, and the reverse proxy is a proxy server, Nginx in the provision of reverse proxy service, by using regular expressions to configure the relevant configuration, take a different forwarding policy, configuration is quite flexible, and in the configuration back-end forwarding requests, completely do not care about the network environment, You can specify any IP address and port number, or other type of connection, request, and so on.
②, load Balancing
This is one of the most common features of Nginx, load balancing, on the one hand, a single burden of load sharing to multiple network nodes to do parallel processing, each node after the end of the processing of the results are aggregated back to the user, which can greatly improve the network system processing capacity On the other hand, a large number of front-end concurrent requests or data traffic is shared across multiple backend network nodes, which effectively reduces the time that front-end users wait. and Nginx load balancer is the latter aspect, mainly on a large number of front-end access or traffic shunt, has ensured the front-end user access efficiency, and can reduce the back-end server processing pressure.
③, Web caching
In many good sites, Nginx can serve as a pre-cache server, which is used to cache front-end requests, thereby improving the performance of the Web server. Nginx makes a local copy of the content that the user has already visited, so that the data is accessed again within a period of time, so there is no need to make a request to the back end of the Nginx server. Reduce network congestion, reduce data transmission delay, improve user access speed.
3, Nginx installation
About Nginx installation, divided into Windows platform and Linux platform installation, Windows version of the Nginx server is more efficient than the Linux version of the Nginx server, and the actual use of the Linux platform is generally the Nginx server 。 So later in the introduction of the Linux version will also be the main.
①
Nginx:http://nginx.org/en/download.html
The development version is mainly used for the development of Nginx software project, and stable release instructions can be put into commercial application as Web server. Here we choose the current stable version:nginx-1.14.0
②, Windows version installation
We extracted the Nginx-1.14.0.zip file downloaded from the previous step to the current directory.
Unzip the directory as follows:
The following is an introduction to the main folders under this directory:
1, CONF directory: The main configuration file to store Nginx, many of the functions are implemented by configuring the directory of nginx.conf files, we will detail later.
2. Docs Directory: The main documents of the Nginx server, including the LICENSE of Nginx server, the LICENSE of OpenSSL, the LICENSE of PCRE and Zlib LICENSE, also includes this version of Nginx server upgrade -level version change instructions, and a README document.
3. HTML directory: two static web files with suffix. html are stored, and these two files are related to the operation of the Nginx server.
4, Logs directory: Store the Nginx server running log files.
5, Nginx.exe: Start the nginx server EXE file, if the Conf directory under the nginx.conf file configuration is correct, through the file can start the Nginx server.
First, start Nginx
Double-click the Nginx.exe file in the directory after the decompression, there is a flash of the screen, the start is successful.
Then enter http://localhost in the browser or http://localhost:80 the following interface appears to start the success.
PS: This page is the index.html file in the HTML directory of the extracted directory above.
second, close Nginx
Go to the directory after unpacking and enter the following command:
1 nginx.exe-s stop
Or you can open the Task Manager, find the Nginx process, right-click End.
③, Linux version installation
The selected Linux system is CentOS6.8.
First, install Nginx environment
1 Yum install gcc-c++2 yum install-y pcre pcre-devel3 yum install-y zlib zlib-de Vel4 yum install-y OpenSSL openssl-devel
For GCC, because the installation of nginx need to first download the source code to compile, compilation depends on the gcc Environment, if there is no gcc Environment, need to install gcc.
for Pcre,prce (perl Compatible Regular Expressions) is a Perl library, which includes a Perl-compatible regular expression library. The Nginx HTTP module uses PCRE to parse the regular expression, so the Pcre library needs to be installed on Linux.
There are many ways to compress and decompress the Zlib,zlib library, and Nginx uses zlib to gzip the contents of the HTTP package, so you need to install the Zlib library on Linux.
For Openssl,openssl is a strong Secure Sockets Layer cipher library that includes key cryptographic algorithms, common key and certificate encapsulation management functions and SSL protocols, and provides a rich set of applications for testing or other purposes. Nginx not only supports the HTTP protocol, but also supports HTTPS (that is, transmitting HTTP on the SSL protocol), so you need to install the OpenSSL library on Linux.
Second, compile and install
First copy the downloaded nginx-1.14.0.tar.gz file to the Linux system and unzip it:
1 tar-zxvf nginx-1.14.0.tar.gz
Then go to the directory after decompression, to compile and install.
1 ./configure--prefix=/usr/local/nginx2make3 make install
Note: Specify the directory that/usr/local/nginx installs for the Nginx service.
Three, start Nginx
Into the/usr/local/nginx directory, the file directory appears as follows:
We then go to the Sbin directory and start nginx with the following command:
./nginx
Of course you can also configure the environment command, so that in any directory can start Nginx.
Linux does not have a message on the good news and does not prompt any information stating that the startup was successful.
Alternatively, you can enter the following command to see if Nginx has a service running:
Ps-ef | grep nginx
Then we enter the IP address of the Linux system in the browser, the interface of Windows installation is successful.
Four, close Nginx
There are two ways of doing this:
Mode 1: Quick Stop
1 cd/usr/local/nginx/sbin2 ./nginx-s stop
This method is equivalent to identifying the Nginx process ID and then using the KILL command to force the kill process. Not very friendly.
Mode 2: Smooth stop
1 cd/usr/local/nginx/sbin2 ./nginx-s quit
This means that the Nginx service is allowed to complete the processing of the network request that is currently being processed, but does not close the connection after receiving a new request, and then stops working.
Five, restart Nginx
Mode 1: Stop and start first
1 ./nginx-s quit2 ./nginx
The equivalent of executing the STOP command before executing the start command.
Method 2: Reload the configuration file
1 ./nginx-s Reload
Usually we use Nginx to modify the most is its configuration file nginx.conf. You can use this command if you want the configuration file to take effect without restarting nginx after the modification.
VI. Detecting configuration file syntax is correct
Mode 1: Specify the configuration file that needs to be checked by the following command
Nginx-t-C /usr/local/nginx/conf/nginx.conf
Mode 2: The nginx.conf configuration file is detected by default by the following command, without the-c parameter.