Nginx installation tutorial and learning of Nginx basic commands

nginx installation tutorial

Related concepts of nginx

basic concept

Nginx is a high-performance HTTP and reverse proxy server. It is very powerful in handling high concurrency and can withstand the test of high load. Some reports show that it can support up to 50,000 concurrent connections.

What can nginx do?

  • reverse proxy
  • load balancing
  • static and dynamic separation

Now let's talk about these three situations.

reverse proxy

Before understanding the reverse proxy, we need to understand what a forward proxy is.

Forward proxy: It is a server located between the client and the original server (origin server). In order to obtain content from the original server, the client sends a request to the proxy and specifies the target (origin server), and then the proxy forwards the request to the original server And return the obtained content to the client. The client can use the forward proxy.

Let me give you an example. We need to make it clear that the proxy is a proxy for something, and the forward proxy is like we want to access Google. Google refuses to accept it if we use a server in China. What if we use a foreign server to access Google? ? It's like our client sends our request to a server in Singapore, and the server in Singapore forwards our request to the Google server, and the Google server sends the response to the Singapore server, and the Singapore server sends Google's response forwarded to our clients. Throughout the process, the Singapore server acts as a proxy for our client to make requests, so that the real client is transparent to Google, and the request sent by the proxy client is a forward proxy.

As shown in the figure below:
insert image description here

After talking about the forward proxy, let’s talk about the reverse proxy, a process of request and response. The participating role is the client. Since the forward proxy is the client, the reverse proxy is the server, and the server is the client. Transparent, let's take a look at the official explanation:

Reverse proxy: The reverse proxy server is located between the user and the target server, but for the user, the reverse proxy server is equivalent to the target server, that is, the user can directly access the reverse proxy server to obtain the resources of the target server.

In a word, the reverse proxy exposes the proxy server address and hides the real server IP address. The real server is transparent to the client . And which reverse proxy server is the nginx server.

As shown in the figure below:

insert image description here

load balancing

The application range of load balancing is very wide. For example, the number of users is very large. One server can no longer handle the requests sent by all users. You can add servers to share the requests, but the requests sent by the client are sent to the nginx server, and the nginx server decides Which server to send the request to later.

As shown in the figure below:
insert image description here

static and dynamic separation

We know that there are static resources of pictures and texts in the server, as well as dynamic resources such as Jsp. The separation of dynamic and static means that those static resources and dynamic resources are placed on different servers. There are special servers to store static resources, and there are also special The server is used to store dynamic resources. The client sends a request, and the nginx server judges whether the request is a dynamic resource or a static resource. After the judgment is completed, it searches for the corresponding static (dynamic) server to request the resource. As shown in the figure below:

insert image description here

nginx installation and basic commands

Installation of nginx under linux system

Attach the official website address of nginx http://nginx.org/

Download the compressed package of nginx we need through the official website, upload the compressed package to the linux server, and start the installation (the installation of nginx requires some dependencies, let's install the dependencies first):

The first step is to download the compressed file of pcre online and depend on:

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

The second step, unzip the compressed file

tar -xvf pcre-8.37.tar.gz

The third step is to enter the pcre-8.37 directory and execute the ./configure command

# 进入pcre的目录
cd pcre-8.37/
# 执行./configure进行检查
./configure

The fourth step is to install using make && make install

make && make install

pcre depends on the installation is complete, you can check whether the installation is successful through pcre-config --version (check the version number command)

pcre-config --version

The fifth step is to install the dependencies of openssl, zlib and gcc

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

The sixth step is to decompress the compressed package of nginx we uploaded

tar -xvf nginx-1.18.0.tar.gz

The seventh step is to enter the directory and execute the ./configure command

# 进入目录
cd nginx-1.18.0
# 进行检查
./configure

Note: I use centos8, so the version used here is nginx-1.18.0. When the version of nginx is too low, it will not support the centos8 environment.

The eighth step is to execute the make && make install command

make && make install

At this point nginx is installed!

A new nginx directory will be added under the /usr/local/ directory after the installation is successful.

We need to open the port corresponding to the firewall when using linux. This is a common command for the firewall under linux:

# 启动防火墙
systemctl start firewalld 
# 关闭防火墙
systemctl stop firewalld
# 查看开放的防火墙端口号
firewall-cmd --list-all
# 开放80端口
firewall-cmd --add-port=80/tcp --permanent
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

Related commands of nginx:

# 进入nginx的sbin的目录中
cd /usr/local/nginx/sbin
# 查看nginx的版本号
./nginx -v
# 启动nginx服务器
./nginx
# 停止nginx服务器
./nginx -s stop
# 重新启动nginx
./nginx -s reload
# 查看nginx是否启动成功
ps -rf | grep nginx

Check the server address: 80 to check in the browser:

insert image description here

So far, the basic commands for installing nginx and nginx have all been completed, click a free follow~~~

Guess you like

Origin blog.csdn.net/weixin_45927121/article/details/121878242