[Nginx from entry to practice] [Basics] 01. What is Nginx

Middleware

* 减少耦合,减少危险操作
* 负载均衡
* 缓存
* 安全防护
* 权限控制

Insert picture description here

Nginx middleware is not limited to business logic, and is effectively independent of the back-end development framework (whether the back-end is Java development, PHP development, or other language frameworks), and can be universal to the platform

Not only emphasizing practice, but also combining principles (such as: Http protocol, operating system), allowing you to understand the principles behind it is more beneficial for you to solve practical problems (such as: bug resolution, secondary development, etc.)

What is Nginx

Nginx (engine x) is an open source, high-performance HTTP and reverse proxy Web server, and it also provides IMAP/POP3/SMTP services."

First, make a brief description of the Web server:

A web server generally refers to a web server, which refers to a program that resides on a certain type of computer on the Internet, which can provide documents to web clients such as browsers, and can also place website files for the world to browse. You can place data files and download them all over the world.

Common web servers are: Apache, Nginx, Microsoft IIS and Tomcat. For example, when I start the Nginx service, the service listens to the port on the server. When accessing the address of this ip+ port from the outside, we can access certain static files on the server, or respond to dynamic services, and process the corresponding http request. And return a result. This is to interact with the web server (that is, Nginx) through the browser.

Nginx is a high-performance web service developed by Russian engineer Igor Sysoev in his spare time as a system administrator in Rambler Group. Official test

Nginx can support 50,000 concurrent links, and the consumption of resources such as cpu and memory is very low, and the operation is very stable, so now many well-known companies are using it

Nginx or secondary development on this basis, including Taobao, Sina, Baidu, etc. For small and medium-sized enterprises, the open source, free and powerful Nginx is definitely the first choice. We will see a set of statistical data to illustrate the wide range of Nginx applications.

Nginx usage scenarios

The usage scenarios of Nginx are as follows:

2.1 HTTP server

As a Web server, Nginx can independently provide Http services. In addition, we often use Nginx as a static resource server to access the static resources on the server. For example, for the latest popular front-end and back-end separation architecture, the front-end is packaged and directly placed at an address. After Nginx is configured, the host can be accessed through Nginx. On the front-end page.

2.2 Reverse proxy

Reverse Proxy refers to the use of a proxy server to accept connection requests on the Internet, then forward the request to the server on the internal network, and return the result from the server to the client requesting the connection on the Internet. At this time, the proxy server acts as a reverse proxy server externally. The advantage of this is that the internal service address will not be exposed, only one public exit will be used uniformly, and the request will be forwarded to different internal services to process the request through URI matching.

2.3 Load balancing

Load balancing is also a high-frequency usage scenario of Nginx. For multiple identical services existing downstream, a certain strategy (random, polling, weight) can be used to send the request to the corresponding service for processing. In this way, due to the existence of multiple identical services, high availability can be achieved. When a service is unavailable, Nginx will automatically discover and remove it from the service cluster, and forward the request to the normal service for processing.

2.4 Third-party plug-ins

Based on third-party plug-ins, Nginx can complete a variety of complex functions to fully satisfy programmers' ideas. For example, the introduction of the lua module in Nginx can implement more fine-grained restrictions on Http requests, including speed limit, current limit, verification and authentication, and so on. Later, OpenResty developed on Nginx has been applied to the microservice gateway direction.

Advantages of Nginx

1. High performance, high stability, low resource consumption

Nginx's process model uses the Master/Workers process pool mechanism, that is, under normal circumstances, Nginx will start a Master process (of course, there can be no master process) and multiple Worker processes to provide external services. The Master process is a monitoring process. It does not process specific TCP and HTTP requests. It is only responsible for receiving UNIX signals and managing the Worker process. It is similar to a contractor on a construction site.

The Worker process is relatively tiring and is responsible for processing client connection requests. It makes full use of the epoll, kqueue and other mechanisms in the Linux system to efficiently process TCP and HTTP requests. Using these characteristics, Nginx has fully tapped the potential of the server and made the server more efficient. Respond quickly to user requests.

In general, 10,000 inactive HTTP Keep-Alive connections only consume 2.5M of memory in Nginx. This is the basis for Nginx to support high concurrent connections and reflects the high-performance characteristics of Nginx. In addition, since the officially provided modules are very stable, each Worker process is relatively independent. When the Woker process fails, the Master process will immediately perceive and quickly pull up a new Worker sub-process to provide uninterrupted services to ensure the stability of the service.

nginx process model

Insert picture description here

2. High scalability

The architecture design of Nginx is very good and very extensible. It is completely composed of multiple modules with different functions, different levels, different types and extremely low coupling.

In addition, we can also perform secondary development on the officially provided modules, such as the HTTP module, in which the HTTP filter module is designed, so that when we develop a new HTTP module, in addition to using the HTTP core module, events module, log module And other different levels of modules, you can also reuse a large number of existing HTTP filter modules intact.

This low-coupling excellent design has created a huge third-party module of Nginx, and Nginx modules are all embedded in binary files for execution, so that third-party modules also have extremely excellent performance and make full use of the high performance of Nginx. Concurrency features, so many high-traffic websites will develop customized modules that meet their own business characteristics based on Nginx, and the development cost is low and the effect is good.

In addition, for small and medium-sized enterprises, Nginx is used out of the box, and its high concurrency capabilities can satisfy most of the business of the enterprise. Therefore, Nginx is widely used in most Internet enterprises.

3. Hot deployment

Supporting hot deployment is a feature of Nginx. Many large portals require 24-hour uninterrupted service. Once the service stops, it is easy to cause user complaints and user loss. Nginx can provide hot deployment function due to the separation design of its management process and worker process, which can meet the requirement of upgrading Nginx on the premise of 7x24 uninterrupted service for large sites.

But it should be noted that Nginx does not support functions such as updating configuration and changing log files without stopping the service. The principle of hot deployment of Nginx is to terminate its connection request without stopping the old process, start a new worker process to handle the new incoming connection request, and then slowly terminate the old worker process to achieve the replacement of the old and the new.

4. Open source and free

Nginx is as open source and free as Big Brother Apache. In today's era, open source can grow vigorously, and free can be used by more companies and individual developers. Of course, Nginx also has its commercial version, Nginx plus, which has richer functions and application scenarios. However, the functions of the free version are already powerful enough, coupled with the endless emergence of third-party plug-in modules, can almost achieve various business functions, coupled with third-party language integration (perl, lua, etc.), for most companies and individuals That is enough.

Of course, the advantages of Nginx definitely don't stop there. Its core function is to maintain efficient services while supporting high concurrent requests. And this is exactly the demand that the majority of developers and major companies urgently need to meet in order to cope with the ever-increasing number of domestic Internet users.

to sum up:

* 多路IO复用
* 使用Epoll模型
* 轻量级
* CPU亲和
* sendfile

Services that nginx can provide

  1. web service.
  2. Load balancing (reverse proxy)
  3. web cache

Advantages of nginx

  1. High concurrency. Static small files
  2. Take up less resources. 20,000 concurrent, 10 threads, memory consumption of hundreds of M.
  3. There are many types of functions. web, cache, proxy. Each function is not particularly strong.
  4. Support the epoll model, so that nginx can support high concurrency.
  5. There is a difference between nginx with dynamic services and Apache. (FASTCGI interface)
  6. Use nginx to limit the IP rate and limit the number of connections.
  7. The configuration is simple and more flexible.

nginx applications

  1. Static server. (Picture, video service) Another lighttpd. Concurrently tens of thousands, html, js, css, flv, jpg, gif, etc.
  2. Dynamic service, nginx-fastcgi way to run PHP, jsp. (PHP concurrency in 500-1500, MySQL concurrency in 300-1500).
  3. Reverse proxy, load balancing. Every day below pv2000W, you can directly use nginx as a proxy.
  4. Cache service. Similar to SQUID, VARNISH.

Nginx features

  1. Based on the asynchronous IO model (epoll, kqueue), it has strong performance and can support tens of thousands of concurrency.
  2. Very good support for small files, high performance (limited to static small files 1M).
  3. The code is beautiful, and the extension library must be compiled into the main program.
  4. The code resource consumption is relatively low.
  5. lighttpd (Baidu Tieba, Douban)
  6. Based on asynchronous IO mode, performance is similar to nginx.
  7. The extension library is SO mode, which is more flexible than nginx.

Guess you like

Origin blog.csdn.net/happy_teemo/article/details/114748240