This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Hello, everyone, my name is du, komori, small as a front end, only need to write code, as for the deployment of related operations, we usually can’t contact, is the so-called professional people do professional thing, in our work, we don’t need to configure, but that doesn’t mean don’t need to know, believe that everyone is more or less heard nginx, so just have a chat today, also please support ~

What is Nginx?

Nginx (Engine X) is a lightweight, high-performance HTTP and reverse proxy server, as well as a general purpose proxy server (TCP/UDP/IMAP/POP3/SMTP), originally written by Igor Sysoev in Russia.

To put it simply:

  • NginxIs a high-performance HTTP and reverse proxy server, which is characterized byLess memory.Strong concurrencyAnd in reality, nginx’s concurrency is better than that of the same type of web server
  • NginxSpecially designed forPerformance optimizationThe most important requirement of development is thatperformanceNginx has been reported to support up to 50,000 concurrent connections

Forward and reverse proxies

Nginx is a reverse proxy server. What is a reverse proxy? Let’s first look at what is a forward proxy

Forward proxy: It is not feasible for computer users in the LAN to access the network directly. They can only access the network through a proxy Server. This proxy service is called forward proxy.

For example, if you and I are together, we can talk directly, but if you and I are separated, we must use a communication device (such as a telephone) to communicate, then this communication device is a “proxy server”, this behavior is called “forward proxy”

So what is a reverse proxy?

Reverse proxy: The client can’t perceive the agent, because the client does not need to configure the access network, as long as the request is sent to the reverse proxy server, chosen by the reverse proxy server to the target server to get data, and then returned to the client, the reverse proxy server and the target server is a server, exposure is a proxy server address, The IP address of the real server is hidden.

In forward agent, I call to you, you can see a phone number to call you, know, I gave you is made by phone number when I use a virtual phone call you in the past, what you see is no longer my phone number, but a virtual number, you don’t know is I’ll give you a dozen, it is called “reverse proxy”.

In the above example to briefly say:

  • Forward proxy: I call you through my mobile phone proxy Server, which means that my mobile phone and I are a whole and separate from your mobile phone (Server)
  • Reverse proxy: I use my mobile phone (proxy Server) to convert it into a virtual number through software to make a call to you. In this case, my mobile phone and your mobile phone are one whole and separate from me

Load balancing

Load balancing: A key component of high availability network infrastructure, typically used to distribute workloads across multiple servers to improve the performance and reliability of websites, applications, databases, or other services.

Without load balancing, the client-server operation is usually as follows: the client requests the server, and the server queries the database and returns the data to the client:

However, with the increasing number of clients and the rapid growth of data and traffic, this situation obviously cannot be met. We can see from the figure above that the requests and responses of the client end are all through the server end. Then, if we increase the number of the server end and share it with multiple servers, can we solve this problem?

But at this time for the client, he went to visit this address is fixed, you don’t have time to pipe the service end, as long as you give me back the data with respect to OK, so we need a “manager”, the service side looking for a boss to come over, the client directly to find eldest brother, assigned by the boss who deal with the data, so as to relieve pressure from the server, The “boss” is the reverse proxy server, and the port number is the server number.

In this way, when there are 15 requests, the reverse proxy server will evenly distribute five requests to the server, which is called load balancing

Dynamic and static separation

When a client initiates a request, the normal situation looks like this:

It’s like when you go to customer service, you usually say a lot of official words first, you ask anything, he will say the same, then this is called static resources (can be understood as HTML, CSS).

When answering specific questions, each answer is different, and these differences are called dynamic resources.

Without separation, every customer can be understood as a first official comments, in play a specific answer, this is increased customer service work, so in order to effectively use the time of the service, we have to separate the official word, a robot, said let him go instead of customer service, thus reduce the workload of the service.

That is, we separate dynamic and static resources and assign them to different servers for parsing, which speeds up parsing and reduces the stress of a single server

Install Nginx

How to install nginx in Linux

Here’s a look at some common commands:

  • View version:./nginx -v
  • Activation:./nginx
  • Close:./nginx -s stop(Recommended) or./nginx -s quit
  • Reload nginx configuration:./nginx -s reload

Nginx configuration file

The configuration file is divided into three modules:

  • Global block: from the configuration file to the Events block, mainly set some configuration instructions that affect the overall operation of the Nginx server. (In the configuration of concurrent processing services, the larger the value is, the more concurrent processing capacity is supported. However, it is subject to hardware and software constraints.)
  • Events block: Affects the network connection between the nginx server and the user. Common Settings include whether to enable serialization of network connections under multiple workProcesses, whether to allow simultaneous reception of multiple network connections, etc
  • HTTP blocks: such as reverse proxies and load balancers are configured here

Location matching rules

There are four ways:

    location[ = | ~ | ~* | ^~ ] url {
    
    }
Copy the code
  • =An exact matchforContains no regular expressionsBefore the URL, require a string with the URLStrict matching, to stop searching down and process the request
  • ^ ~: used toContains no regular expressionsBefore asking the NGIN server to find the representationThe MATCHING degree between URL and string is the highestImmediately after the location is used to process the request without a match
  • ~The best matchIs used to represent urlsInclude regular expressionsAnd,Distinguish betweenCase sensitive.
  • ~ *And:~Same thing, exceptDoes not distinguish betweencase

Note:

  • ifurlIf regular expressions are included, no need is required ~At the beginning
  • Nginx matching hasprioritiesOnce a match is made, it will exit immediately and no longer conduct downward matching

End

For detailed configuration, see: Nginx tutorial for the front end

The special knowledge about Nginx has been completed. If you like, 👍🏻, the special knowledge about Nginx has been completed. The special knowledge about Nginx has been completed.