Nginx reverse proxy ---- teach you how to do ssm+springboot introductory back-end project dark horse programmer Ruiji takeaway (11)


foreword

In order to consolidate the knowledge learned, the author tried to start publishing some blogs of learning notes for future review. Of course, it would be great if it could help some newcomers learn new technologies. The author is a piece of food. If there are any mistakes in the records in the article, readers and friends are welcome to criticize and correct.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

1. Overview of Nginx

1. Introduction to Nginx

  1. Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server. It is characterized by less memory and strong concurrency capabilities. In fact, the concurrency capability of nginx is better than other web servers of the same type. Websites using nginx in mainland China include: Baidu, Jingdong Sina, Netease, Tencent, Taobao, etc.
  2. Nginx was developed by Igor Sysoyev for the second most visited Rambler, ru site in Russia (Russian: Pam6nep). The first public version. 1.0 was released on October 4, 2004.
  3. Official website: https://nginx.org/

2. Nginx download and install

  1. Install dependent packages yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

insert image description here

  1. Download the Nginx installation package wget https://nginx.org/download/nginx-1.16.1.tar.gz

insert image description here

insert image description here

  1. Unzip tar -zxvf nginx-1.16.1.tar.gz

insert image description here

  1. cd nginx-1.16.1

insert image description here

  1. ./configure --prefix=/usr/local/nginx

insert image description here

  1. make && make install

insert image description here

3. Nginx directory structure

  1. After installing Nginx, let's get familiar with the directory structure of Nginx. The key directories/files are as follows:
  1. conf/nginx.conf ---- nginx configuration file
  2. html ---- Store static files (html, CSS, Js, etc.)
  3. logs ---- log directory, store log files
  4. sbin/nginx ---- binary file, used to start and stop Nginx service

insert image description here

Two, Nginx command

1. View version

  1. To view the Nginx version, you can use the command

./nginx -V

insert image description here

2. Check the correctness of the configuration file

./nginx -t ---- Before starting the Nqinx service, you can check whether there is any error in the configuration of the conf/nginx.conf file. The command is as follows:

insert image description here

3. Start and stop

1. Start the Nginx service using the following command

./nginx

2. Stop the Nginx service using the following command

./nginx -s stop

3. After the startup is complete, you can view the Nginx process

ps -ef | grep nginx

insert image description here

4. Reload configuration file

./nginx -s reload

insert image description here

Three, Nginx configuration file structure

The Nginx configuration file (conf/nginx.conf) is divided into three parts as a whole

  1. Global block ---- configuration related to network connection
  2. events block ---- configuration related to network connections
  3. http block ---- proxy, cache, logging, virtual host configuration
    . http global block
    . Server block
    ---- Server global block
    ---- location block
  4. Note: Multiple Server blocks can be configured in the http block, and multiple location blocks can be configured in each Server block.

insert image description here

insert image description here

4. Specific application of Nginx

1. Deploy static resources

1. How does Nginx deploy static resources

  1. Nginx can be used as a static web server to deploy static resources. Static resources refer to some files that actually exist on the server side and can be displayed directly, such as common htm [pages, css files, js files, pictures, videos and other resources.
  2. Compared with Tomcat, Nginx is more efficient in handling static resources, so in a production environment, static resources are generally deployed to Nginx. Deploying static resources to Nginx is very simple, just copy the files to the html directory under the Nginx installation directory.

2. Examples

insert image description here
insert image description here

2. Reverse proxy

1. Forward Proxy

  1. 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 returns the obtained content to the client.
  2. A typical use of a forward proxy is to provide a way for LAN clients inside the firewall to access the Internet.
  3. Forward proxy generally sets up a proxy server on the client side, forwards the request through the proxy server, and finally accesses the target server

insert image description here

2. Reverse proxy

  1. 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, and the reverse proxy server is responsible for forwarding the request to the target server.
  2. The user does not need to know the address of the target server, and does not need to make any settings on the client side.

insert image description here

3. Example of configuring reverse proxy

insert image description here

insert image description here

3. Load balancing

1. What is load balancing

  1. The early website traffic and business functions were relatively simple, and a single server could meet the basic needs. However, with the development of the Internet, the business traffic is increasing and the business logic is becoming more and more complex. The performance of a single server and the problem of single point of failure have become prominent. Therefore, multiple servers are required to form an application cluster to horizontally expand performance and avoid single point failures.
  2. Application cluster: Deploy the same application to multiple machines to form an application cluster, receive requests distributed by the load balancer, perform business processing and return response data
  3. Load balancer: Distribute user requests to a server in the application cluster for processing according to the corresponding load balancing algorithm

insert image description here

2. Load balancing configuration example

insert image description here
insert image description here

3. Load balancing strategy

name illustrate
polling default method
weight weight method
ip_hash According to the ip allocation method
least conn According to the least connection method
url hash According to the url distribution method
fair By response time method

Summarize

Everyone is welcome to leave a message for exchange and criticism. If the article is helpful to you or you think the author's writing is not bad, you can click to follow, like, and bookmark to support.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, please feel free to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/131850997