Nginx learning 2: Nginx installation configuration and common commands

Nginx installation, common commands and configuration files

Install Nginx on Linux System

We use a virtual machine to complete the steps of installing Nginx on the Linux system. Here I choose the Linux system of CentOS7.

1. Go to the official website to download the Nginx
official website address: http://nginx.org/en/download.html
insert image description here
We choose the direct download of the stable version.

2. Dependencies required to install Nginx
First we install the pcre dependency, we pick a directory and execute the following code in the console

wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz

Note: If you can't download it, you may need root permission. Use su root to upgrade the permission.
After downloading the installation package, decompress it, enter the decompressed directory, and execute

./configure

At this time, an error may be reported, as shown below:
insert image description here
This is because there is no C++ compiler in your virtual machine, we just download a gcc

yum -y install gcc-c++

Then proceed to execute the following command

make && make install

Compile and install, so far, the installation of pcre dependencies is complete.
We can use the pcre-config --versioncommand to see if the installation was successful.

Then other dependencies we can use the following command to download and install

yum -y install make zlib zlib-devel libtool openssl openssl-devel

3. Finally, we install the Nginx we downloaded from the official website

Put the installation package under the corresponding directory and decompress it, enter the decompression directory, and execute the following command

./configure
make && make install

Note: After executing the make && make install command, an error may be reported, as shown in the figure below:
insert image description here
This is because the permissions are not enough, we can switch the root permissions

After switching root permissions, install again, the installation is successful

Nginx startup and access

After the installation is complete, we start nginx, go to the /usr/local/nginx/sbin directory, and execute

./nginx

In this way, the nginx service has been started, as shown in the figure below:
insert image description here
We enter the ip address of the virtual machine in the host browser to access nginx, and the result is as follows:
insert image description here
Um...
firewall problem, the default port 80 of nginx is not allowed to open in our virtual machine access wall, We then execute the following commands

# 查看开放的端口号
firewall-cmd --list-all
# 设置开放的端口号
firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-port=80/tcp --permanent
# 重启防火墙
firewall -cmd --reload

As shown below:
insert image description here

Nginx common commands

The prerequisite for us to use the nginx operation command: we must enter the nginx directory
/usr/local/nginx/sbin

# 查看 nginx 的版本号
./nginx -v

# 启动 nginx
./nginx

# 关闭 nginx
./nginx -s stop

# 重新加载 nginx
./nginx -s reload

Nginx configuration file

Nginx configuration file location

/usr/local/nginx/conf/nginx.conf

Basic composition of Nginx configuration file

There are a lot of comments starting with "#" in the configuration file. We remove all paragraphs starting with #, and the simplified
content is as follows:
insert image description here
According to the above files, we can clearly divide the nginx.conf configuration file into three parts:

Part 1: Global Blocks

From the beginning of the configuration file to the content between the events block, some configuration instructions that affect the overall operation of the nginx server will be set, mainly including configuring the user (group) running the Nginx server, the number of worker processes allowed to be generated, the process PID storage path, and the log. Storage path and type and introduction of configuration files, etc.
For example, the configuration in the first line above:
insert image description here
This is the key configuration of the Nginx server concurrent processing service. The larger the worker_processes value is, the more concurrent processing it can support, but it will be restricted by hardware, software and other equipment.

Part 2: The events block

For example, the above configuration:
insert image description here
The instructions involved in the events block mainly affect the network connection between the Nginx server and the user. Common settings include whether to enable serialization of network connections under multiple work processes, whether to allow multiple network connections to be received at the same time, and which one to choose. An event-driven model to handle connection requests, the maximum number of simultaneous connections each word process can support, etc.
The above example shows that the maximum number of connections supported by each work process is 1024.
This part of the configuration has a great impact on the performance of Nginx, and should be flexibly configured in practice.

Part 3: http blocks

insert image description here
This is the most frequent part of Nginx server configuration, where most functions such as proxy, cache, and log definitions and third-party modules are configured.
It should be noted that http blocks can also include http global blocks and server blocks.

http global block

The directives of http global block configuration include file import, MIME-TYPE definition, log customization, connection timeout time, upper limit of single-link requests, etc.

server block

This is closely related to the virtual host. From the user's point of view, the virtual host is exactly the same as an independent hardware host. This technology is produced to save the cost of Internet server hardware.
Each http block can contain multiple server blocks, and each server block is equivalent to a virtual host.
And each server block is also divided into a global server block, and can contain multiple location blocks at the same time.

  • Global server block
    The most common configuration is the listening configuration of this virtual machine host and the name or IP configuration of this virtual host.
  • location block
    A server block can be configured with multiple location blocks.
    The main function of this block is based on the request string received by the Nginx server (such as server_name/uri-string), for strings other than the virtual host name (or IP alias) (for example: the previous /uri-string) Match to process a specific request. Functions such as address orientation, data caching, and response control, as well as the configuration of many third-party modules, are also performed here.

PS: You can also go to my personal blog to see more content
Personal blog address: Xiaoguan classmate's blog

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/120580603