Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx principle

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Nginx process model

In the mode of work, Nginx is divided into two modes: single-process and multi-process. In the single worker process mode, in addition to the main process, there is also a worker process, which is single-threaded; in the multi-worker process mode, each worker process contains multiple threads. Nginx defaults to single worker process mode.

After Nginx starts, there will be one master process and multiple worker processes.

Master process

The master process acts as the interactive interface between the whole process group and the user and monitors the process at the same time. It does not need to deal with network events and is not responsible for business execution. It will only manage worker processes to restart services, smooth upgrades, change log files, and take effect in real time.

Worker process

The basic network events are handled in the worker process. The number of worker processes can be set. In general, we will set the same number of cores as the machine cpu.

A request can only be processed in one worker process, and it is impossible for a worker process to process requests from other processes.

What can nginx do?

Reverse proxy

Forward agent

Load balancing

HTTP server (including static and dynamic separation)

Forward agent

Before we talk about reverse proxy, let's take a look at the forward agent. Forward agent is also the most common agent model that everyone comes into contact with. We will talk about the processing mode of forward agent from two aspects. Explain what is a forward agent from the aspects of software and life respectively.

In today's network environment, if we need to visit some foreign websites because of technical needs, you will find that there is no way for us to visit a website located abroad through a browser. At this time, everyone may use an operation FQ to visit. The main way to FQ is to find a proxy server that can access foreign websites, and we will send the request to the proxy server. Proxy server to visit foreign websites, and then pass the data to us!

The most important feature of the forward proxy is that the client is very clear about the server address to be accessed; the server only knows which proxy server the request comes from, but not which specific client; the forward proxy mode shields or hides the real client information.

Forward agent. To put it simply, I am a user, and I cannot access a website directly, but I can access a proxy server, which can access a website that I cannot access, so I connect to the proxy server first. Tell it that I need the content that cannot be accessed from the site, and the proxy server will retrieve it and return it to me. From the point of view of the website, there is only one record when the proxy server comes to pick up the content. The conclusion is that a forward proxy is a server between the client and the original server (origin server). In order to get content from the original server, the client sends a request to the agent and specifies the target (the original server), and then the agent transfers the request to the original server and returns the obtained content to the client. The client must make some special settings to use the forward proxy.

Reverse proxy. For example, I want to visit the localhost:8080/views/test1 page, but there is no test1 resource on the server corresponding to view, it is a resource called from another server. In this way, the server corresponding to view uses a reverse proxy. That is, users only need to send the request to a specific reverse proxy server, the specific request is handled by the user does not need to know (in fact, do not know), by the proxy server unified processing. The conclusion is that the reverse proxy is just the opposite, it is like the original server to the client, and the client does not need to make any special settings. The client sends a normal request to the content in the reverse proxy's namespace (name-space), which then determines where to forward the request (the original server) and returns the obtained content to the client as if it were already its own.

A typical use of forward proxies is to provide access to Internet for LAN clients in firewalls. Forward agents can also use buffering features to reduce network usage.

A typical use of a reverse proxy is to provide a server behind a firewall for Internet users to access. Reverse proxies can also provide load balancing for multiple servers at the back end, or buffering services for slower servers at the back end.

Summary

Forward proxy: for the client, the proxy server proxies the client, forwards the request, and returns the obtained content to the client.

Reverse proxy: for the client, the proxy server is like the original server, and the web node server of the proxy cluster returns the result.

Nginx load balancing

Load balancing is also a common function of Nginx, such as separating database servers and pictures as separate services. These are simple data load balancing, which means to distribute the pressure to different machines for execution. Nginx can be used not only as a powerful web server, but also as a reverse proxy server. In load balancing configuration, reverse proxy is generally required. Jump to load balancing through a reverse proxy. Nginx currently supports three load balancing strategies and two commonly used third-party strategies.

Load balancing functions:

Forwarding

Fault removal

Restore add

High availability

RR

Load is carried out according to polling (default). Each request is allocated to a different backend server one by one in chronological order. If the backend server down is dropped, it can be automatically eliminated. Although this method is simple and cheap. But the disadvantages are: low reliability and uneven load distribution.

Weight

Specify the polling probability. The weight is proportional to the access ratio, which is used in the case of uneven performance of the backend server.

Ip_hash

There is a problem with both the above two methods, that is, when the next request comes, the request may be distributed to another server. When our program is not stateless (using session to save data), there is a big problem. For example, if you save the login information to session, you need to log in again when you jump to another server. So in many cases, we need a customer to access only one server, so we need to use iphash. Each request of iphash is allocated according to the hash result of accessing ip, so that each visitor accesses a back-end server regularly, which can solve the problem of session.

Fair (third party)

Requests are allocated according to the response time of the back-end server, and priority is given to those with short response time.

Url_hash (third party)

Allocate requests according to the hash result of accessing url, so that each url is directed to the same backend server, which is more effective when the backend server is cached. Add hash statement to upstream. Other parameters such as weight cannot be written in server statement. Hash_method is the hash algorithm used.

HTTP server

Nginx itself is also a server of static resources. When there are only static resources, Nginx can be used as a server. At the same time, it is also very popular to separate dynamic and static resources, which can be realized through Nginx. Dynamic and static separation allows dynamic web pages in dynamic websites to distinguish immutable resources from frequently changing resources according to certain rules. We can cache static resources according to their characteristics, which is the core idea of website static processing.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report