This is the 21st day of my participation in the August More Text Challenge
The middleware
* Reduce coupling, reduce dangerous operations * load balancing * cache * security protection * permission controlCopy the code
Nginx middleware, not limited to business logic, is effectively independent of the backend development framework (whether the backend is Java development, PHP development, or other language framework) can achieve platform common
It not only focuses on practice, but also combines principles (e.g., Http protocol, operating system) so that you can understand the principles behind it and solve practical problems (e.g., bug solving, secondary development, etc.).
What is the Nginx
Nginx (Engine X) is an open source, high-performance HTTP and reverse proxy Web server that also provides IMAP/POP3/SMTP services.
First, a brief description of the Web server:
A Web server generally refers to a Web server, which is a program that resides on a certain type of computer on the Internet and can provide documents to Web clients such as browsers, as well as place Web files for the world to browse. You can place data files for the world to download.
Common Web servers are Apache, Nginx, Microsoft IIS, and Tomcat. For example, when I start the Nginx service, the service listens for the port on the server. When we access the IP + port address from outside, we can access some static files on the server, or dynamic service response, process the corresponding HTTP request and return a result. This is done by interacting with a Web server (also known as Nginx) through a browser.
Nginx is a high-performance Web service developed by Igor Sysoev, a Russian engineer working as a system administrator at Rambler Group, in his spare time
Nginx can support up to 50,000 concurrent links with very low CPU, memory and other resource consumption, so it runs very stable, so many well-known companies are using it now
Nginx or on this basis for secondary development, including Taobao, New wave, Baidu, etc. For small and medium enterprises, the open source, free and powerful Nginx is certainly the first choice, and we’ll see a set of statistics to illustrate the wide adoption of Nginx.
Nginx usage scenarios
Nginx can be used in the following scenarios:
2.1 HTTP Server
As a Web server, Nginx can provide Http services independently. 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 back-end separation architecture, the front end is packaged directly into a certain address, after Nginx configuration can access the front-end page on the host through Nginx.
2.2 Reverse Proxy
In Reverse Proxy mode, a Proxy server receives Internet connection requests, forwards the requests to the Intranet server, and returns the results to the Internet client. In this case, the proxy server acts as a reverse proxy server. The advantage of this is that the internal service addresses will not be exposed and only a common exit will be used to forward requests to different internal services through URI matching.
2.3 Load Balancing
Load balancing is also a high frequency usage scenario of Nginx. For multiple same services downstream, requests can be sent to the corresponding service using a certain strategy (random, polling, weight). When a service becomes unavailable, Nginx will automatically detect 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 perform a variety of complex functions to satisfy the programmer’s desire. For example, the introduction of the Lua module in Nginx enables more fine-grained restrictions on Http requests, including speed limiting, traffic limiting, verification authentication, and so on. Subsequently, OpenResty developed on Nginx has been applied in the direction of microservices gateway.
The advantage of Nginx
1. High performance, high stability and low resource consumption
The process model of Nginx adopts the mechanism of Master/Workers process pool, that is, usually, Nginx will start a Master process (of course, there can be no Master process) and multiple Worker processes to provide services. The Master process is a monitoring process that does not process specific TCP and HTTP requests. It is only responsible for receiving UNIX signals and managing Worker processes, similar to a contractor at a site.
Worker processes are tired and are responsible for processing client connection requests. It makes full use of epoll, Kqueue and other mechanisms in The Linux system to efficiently process TCP and HTTP requests. Using these features, Nginx fully exploits the potential of the server and makes the server respond to user requests faster.
On average, 10,000 inactive HTTP keep-alive connections consume only 2.5M of memory in Nginx. This is the foundation of Nginx’s support for high concurrent connections, which demonstrates the high performance of Nginx. In addition, as the officially provided modules are very stable, each Worker process is relatively independent. When the Woker process fails, the Master process will immediately sense and quickly pull the new Worker child process to provide services continuously, ensuring the stability of the service.
Nginx process model
2. High scalability
Nginx architecture design is very excellent, very extensible, it is completely composed of a number of different functions, different levels, different types of modules and very low coupling.
In addition, we can also carry out secondary development on the officially provided modules, such as HTTP module, in which HTTP filter module is designed, so that when we develop a new HTTP module, besides using modules of different levels, such as HTTP core module, Events module, log module, etc., You can also reuse a large number of existing HTTP filter modules intact.
This excellent design of low coupling makes Nginx huge third-party modules, and the modules of Nginx are embedded in binary files, so that the third-party modules also have extremely excellent performance, take full advantage of Nginx’s high concurrency characteristics. Therefore, many high-traffic websites will develop customized modules based on Nginx in line with their own business characteristics, and the development cost is low, the effect is good.
In addition, for small and medium-sized enterprises, Nginx is out of the box, and its high concurrency capability can meet most of the business of enterprises, so Nginx is widely used in most Internet enterprises.
3. Hot deployment
Support for hot deployment is a feature of Nginx. Many large portals require round-the-clock service, which can easily lead to user complaints and user loss once the service stops. Due to the separation of management process and Worker process design, Nginx can provide hot deployment function, so that large sites can upgrade Nginx under the condition of 7×24 hours of uninterrupted service.
Note, however, that Nginx does not support configuration updates, log file changes, and so on without stopping the service. The hot deployment principle of Nginx is to terminate the connection request of the old process without stopping it, and start the new Worker process to process the new connection request, and then slowly terminate the old Worker process to achieve the replacement of new and old.
Open source and free
Like big brother Apache, Nginx is open source and free. In this day and age, open source can thrive and free can be used by more enterprise and individual developers. Of course, Nginx also has its commercial version, Nginx Plus, which has richer features and application scenarios. However, the functionality of the free version is powerful enough, and the endless plugin modules of the third party can achieve almost any business function, and the language integration with the third party (Perl, Lua, etc.) is sufficient for the vast majority of enterprises and individuals.
Of course, Nginx definitely has more advantages than that. Its core function is to maintain efficient service while supporting high concurrency requests. This is a need that developers, as well as companies, urgently need to meet in order to cope with the growing number of Internet users in China.
Conclusion:
* Multiplexing IO * using the Epoll model * lightweight * CPU affinity * sendFileCopy the code
Nginx can provide services
- The web service.
- Load Balancing (Reverse proxy)
- Web Cache
The advantages of nginx
- High concurrency. Static small file
- Occupies less resources. 20,000 concurrent, 10 threads, memory consumption of several hundred megabytes.
- There are many kinds of functions. Web cache, the proxy. None of them are particularly powerful.
- Support for the epoll model enables nGINx to support high concurrency.
- Nginx with dynamic services is different from Apache. (FASTCGI interface)
- Using Nginx can limit IP speed, you can limit the number of connections.
- Simple configuration and more flexibility.
Nginx applications
- Static server. (Photo, video service) another Lighttpd. Concurrent tens of thousands of HTML, JS, CSS, FLV, JPG, GIF and so on.
- Dynamic service, nginx – FastCGI way to run PHP, JSP. (PHP concurrency is 500-1500, MySQL concurrency is 300-1500).
- Reverse proxy, load balancer. Pv2000W, can directly use nGINx as a proxy.
- Caching services. A similar squids, VARNISH.
Nginx features
- Based on asynchronous IO model, (epoll, Kqueue), strong performance, can support tens of thousands of concurrent.
- Very good support for small files, high performance (limited to static small files 1M).
- The code is beautiful and the extension library must be compiled into the main program.
- Code resource consumption is low.
- Lighttpd (Baidu Post Bar, Douban)
- Based on asynchronous IO mode, performance is similar to nginx.
- The extension library is in SO mode, which is more flexible than Nginx.