Because of Nginx’s deep mining of hardware and operating system kernel features, it achieves high throughput while maintaining high concurrency. Excellent module design, make Nginx ecosystem, extremely rich. A large number of third-party modules make it easy for Nginx to implement customization requirements in various scenarios
These features make Nginx standard for Internet companies
There are three main application scenarios for Nginx
Often, after a Web request comes down from the red arrow, it goes through Nginx, to the application service, and then to a database like Redis or MySQL that provides basic data functionality
There is a problem, application service for special high requirement to the development efficiency, its efficiency is very low, its QPS, TPS or concurrent is limited, so you need to put a lot of such application service to form a cluster, to provide customers with high availability, and once a lot of application service cluster, Nginx is required to have reverse proxy capabilities that can pass dynamic requests to application services
A cluster of many application services, which must bring two requirements:
- Dynamic capacity expansion is required
- When some services fail, disaster recovery (Dr) is required
This makes it necessary for the reverse proxy to have load balancing. Secondly, in such a link, Nginx is on the brink of enterprise internal network nodes, as the growth of the network link, the user experience of time delay will increase, so if we can get some users to look the same, or for a period of time looks the same dynamic content, cached in Nginx part, directly by the Nginx provide access to the user. Then the user access latency will be reduced a lot
So reverse proxies spawn another feature called caching, which speeds up access. Most of the time we are accessing CSS, JS or some small icon images, such static resources, is not necessary by the application service to provide access, it only needs to be through the local file, the system placed on the static resources, directly provided by Nginx access can be, this is Nginx provides static resource service function
The third application scenario is derived from the performance of the application service itself because of many problems, but the database service is much better than the application service, because its application scenario is simpler, its concurrency performance and TPS are much higher than the application service, so the third application scenario is derived. Nginx directly accesses the database (Redis or similar services) and uses Nginx’s powerful concurrency implementation, such as web firewalls, to provide users with complex business functions. This requires very powerful business processing capabilities of API services, so languages like OpenResty, or integrated JavaScript like Nginx, leverage JavaScript, Lua, and some of the tools that come with their languages to provide full API services
The historical background to the emergence of Nginx
- The rapid growth of Internet data volume
- Moore’s Law: Performance increases
- Inefficient Apache
The rapid development of globalization and the Internet of Things has led to a rapid rise in the number of people and devices accessing the Internet. The rapid explosion of data has put forward high requirements on the performance of hardware. When it comes to hardware, we all know Moore’s Law. My service was running on a 1GHz CPU, and after a year and a half, when I upgraded to a 2GHz CPU, I could predict that my service would double its performance
But by the beginning of this century, Moore’s law had broken down for the frequency of a single CPU, and cpus began to move toward multiple cores. At this point, when your service is running on an 8-core CPU, and a year later, you switch to a 16-core CPU, your service usually doesn’t double its performance
So where are these performance losses?
The main problem is that operating systems and a lot of software are not prepared for multi-core architectures such as Apache. Apache is inefficient because of its architectural model, in which a process only processes one connection and one request at a time, and only then does the next request
What’s the subtext? This is actually in the process of using the operating system to switch between features, because the operating system on microscopic limited CPU, but the operating system is designed to serve hundreds or even thousands of process at the same time, the Apache a process can only be in the service of a connection, such a model, cause, when Apache need facing hundreds of thousands, millions of connection, There is no way to open millions of processes, and switching between processes is too costly. As the number of concurrent connections increases, the performance cost of these interprocess switches increases. Nginx was built specifically for this scenario. Nginx can handle millions or even tens of millions of concurrent connections
The advantages of Nginx
- High concurrency and high performance
- Good scalability
- High reliability
- Hot deployment
- BSD license
Its Y-axis is the number of requests processed per second (RPS) and its X-axis is the number of concurrent connections. As you can see from the graph, for most applications or Web servers, the RPS drops sharply as the number of concurrent connections increases, which is the problem with the architecture mentioned above
The first advantage of Nginx is the combination of high concurrency and high performance, which can be achieved by using as little memory as possible for each connection. Having high concurrency and high performance often requires very good design. What standards can Nginx measure up to? For example, today’s more mainstream servers (32 cores, 64 gigabytes of memory) can easily reach tens of millions of concurrent connections and 100W RPS for simple static resource requests
The second core advantage is that it is very extensible. Its scalability is mainly reflected in its modular design, modular design is very stable, so that Nginx third-party module ecosystem is very rich. The rich ecosystem ensures the rich functionality of Nginx
The third advantage is its high reliability. High reliability means that Nginx can run continuously on the server for years, while many Web servers run for weeks or months and require a restart. Nginx is a high concurrency, high performance reverse proxy server, it often runs on the edge of the enterprise node, at this time, if the enterprise wants to provide 4 9, 5 9, or even higher high availability, for Nginx continuous running, can be down time in a year may only be measured in seconds. So in such a role, Nginx’s high reliability provides a very good guarantee
The fourth advantage is hot deployment. Nginx is the ability to upgrade Nginx without stopping the service, which is very important for Nginx because there are millions of concurrent connections running on Nginx. If it is a common service, we may need to kill the process and then restart it. For Nginx, however, killing the Nginx process will cause the operating system to send a TCP reset packet to all connected clients, and many clients will not be able to handle the reset request well. Hot deployment is necessary in large concurrency scenarios, where accidental events can lead to inevitable malign results
The fifth advantage is the BSD license. It means that not only is Nginx open source and free, but you can modify the source code of Nginx under custom scenarios, which is legal in commercial scenarios
Nginx has four main components
Nginx binary executable file
This is a file built from Nginx’s own framework, its official modules, and various third-party modules compiled into it. This file is the equivalent of the car itself, it has a complete system, all functions are provided by it
Nginx.conf configuration file
It acts as a driver, and while the binary executable already provides many functions, it is up to the nginx.conf configuration file to determine whether those functions are turned on, and once turned on, what behavior is defined to handle requests. So it’s the driver, controlling the behavior of the car
Access. log Access logs
It creates a GPS track of all the places the car passes, and access.log records every HTTP request and response
Error. log Error log
It is the same as the black box. When unexpected problems occur, you can locate the problems through error. Log
Nginx release status
- Feature: What features are published
- Bugfix: What bugs have been fixed
- Change: What little refactoring was done