2.1 installation

2.1.1 Preparations for installation

The Nginx installation requires the determination of several libraries associated with the Linux installation. Otherwise, the configuration and compilation will be incorrect.

  1. Whether the GCC compiler is installed

    Check for installation:

    yum list installed | grep gcc
    Copy the code

    Perform installation:

    yum install gcc -y
    Copy the code
  2. Check whether the OpenSSL library is installed

    Check for installation:

    yum list installed | grep openssl
    Copy the code

    Perform installation:

    yum install openssl openssl-devel -y
    Copy the code
  3. Check whether the PCRE library is installed

    Check for installation:

    yum list installed | grep pcre
    Copy the code

    Perform installation:

    yum install pcre pcre-devel -y
    Copy the code
  4. Whether the Zlib library is installed

    Check for installation:

    yum list installed | grep zlib
    Copy the code

    Perform installation:

    yum install zlib zlib-devel -y
    Copy the code
  5. To install the software once, run the following command
    yum install gcc openssl openssl-devel pcre pcre-devdl zlib zlib-devel -y 
    Copy the code

2.1.2 Official Installation

Decompress the downloaded nginx file and run the tar -zxvf nginx-1.14.2.tar.gz command

Switch to the decompressed nginx home directory and run the CD nginx-1.14.2 command

Run the./configure –prefix=/usr/loacl/nginx command in nginx-1.14.2

(where –prefix specifies the nginx installation path) Note: No Spaces around the equals sign

Run the make command to compile the file

Run the make install command to install

After the installation is complete, you can switch to the /usr/local/nginx directory to view the contents

Nginx contains the following files: conf (nginx configuration file), HTML (web page address), logs (nginx startup file), sbin (nginx startup file);Copy the code

2.2 start

2.2.1 Common Boot

Switch to the sbin directory of the nginx installation directory and run./nginx

Step 1: CD sbin/ Step 2: PWD Step 3:./nginx Run the ps -ef | grep nginx process command to check whether the nginx process contains the master process and worker processCopy the code

2.2.2 Using the Configuration File for Startup

./nginx -c /usr/local/nginx/conf/nginx.conf
Copy the code
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Copy the code

-c specifies the configuration file and the configuration file path must be an absolute path

2.2.3 Checking whether Nginx is started

Run the ps -ef | grep nginx command to query the nginx process

The Nginx architecture consists of the master process and its worker

The master process reads the configuration file and maintains the worker process, which actually handles the requests

After Nginx starts, some TMP files will appear in the installation directory. These files are temporary files.

2.3 shut down

2.3.1 Gracefully closing Nginx

Run ps -ef | grep nginx to find the nginx process number

Run the kill -quit active Pid command

Note: 1. Pid is the master process PID (PID) of the master process number, and other are worker process pid (pid) of child processes.

2. This type of shutdown will be closed after the request is processed, so it is called an elegant shutdown

2.3.2 Quickly Closing Nginx

Find the nginx process number: ps – ef | grep nginx

Run the kill -term active PID command

> Note: 1. Pid is the master process ID, and other worker processes are worker processes. This method of closing, whether the request is completed or not, is more violent, called fast closingCopy the code

2.3.3 restart Nginx

./nginx -s reload
Copy the code

2.4 Checking the Configuration

After modifying the Nginx configuration file, you can run the Nginx command to check the syntax of the configuration file.

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -t
Copy the code

2.5 other

On Linux view nginx version: / usr/local/nginx/sbin/nginx – V

-v (lowercase v) Displays the nginx version

-v (uppercase V) displays the nginx version, compiler version, and configuration parameters

3.1 Nginx core configuration file

To learn about Nginx, you need to be familiar with its core configuration file, named nginx.conf, which is located in the Nginx installation directory /usr/local/nginx/conf.

For details, see nginx configuration in the resources directory. Conf.

3.1.1 Basic Configuration

Configure the worker process running user. Nobody is also a Linux user and is used to start applications without a password

#user nobody;

Configure the number of worker_processes, which is usually equal to or twice the number of cpus. Worker_processes 1

[debug | info | notice | warn | error | crit]. The default value is error error_log logs/error.log.

#error_log logs/error.log notice;

#error_log logs/error.log info;

Pid logs/nginx.pid # Configure the process PID file

3.1.2 events configuration

> Configure work mode and link book > >events {> worker_connections 1024; Worker_processes * worker_connections specifies the maximum number of connections per worker process. >} > ` ` `Copy the code

3.2 Main applications of Nginx

  • Static Site deployment
  • Load balancing
  • Static agent
  • Dynamic and static separation
  • Virtual host

4. Static website deployment

Nginx is an HTTP Web server that can send static files (such as HTML and images) back to the browser client through THE HTTP protocol.

 location / {
            root   html;
            index  index.html index.htm;
        }
        
  location /ace {
            root   /opt/www;  
            index  index.html index.htm;
        }
Copy the code

Root indicates that the root path of the project is (IP + port), and / = /opt/ WWW. “/ ace” = “/ opt/WWW/ace”

5. Load balancing

5.1 Overview of Load Balancing

In the early days of the web, we generally used a single machine to provide centralized services externally. With the increase of business volume, one server is not enough for us, so we will form several machines into a cluster to provide services externally. However, there is usually only one access to our website, such as www.web.com. So how do you distribute your requests to different machines in the cluster when a user accesses www.web.com in the browser? That’s what load balancing does.

Load balancing usually refers to the “even” distribution of requests across multiple server nodes in a cluster, which in this case means that they are nearly even over a relatively large statistical range, but not completely so.

5.2 Implementation mode of load Balancing