Learn nginx--1 from now on. Basic knowledge

Source: Internet
Author: User
Tags epoll unix domain socket ruby on rails
learn nginx--1 from now on. Basic Knowledge

As a high-speed, lightweight, high-performance, such as the advantages of a server, Nginx in recent years, the rapid development and expanding market share, and even in the recent market share more than Microsoft's IIS, jumped to the second, after Apache.

However, due to its high-performance characteristics, especially for high-traffic sites, in the world's top 10,000 sites in the market share of 58.4%, stable first.

The birth of Nginx

Nginx, an HTTP site server developed by the Russian Igor Sysoev in 2002, is claimed to be able to handle 500 million requests a day. It was made public in 2004, using the BSD Open source protocol, which runs on almost all the mainstream operating systems.

In the age of Nginx, there was a problem in the software world that was most hotly discussed, and that was the c10k question (how to solve the concurrent request problem of 100,000 clients).

Even though the hardware specifications are still rising, if, as with Apache HTTP, each request is divided into a process or thread to execute, the process number or the thread stack will inevitably run out of system resources. Therefore, the lightweight level server is imperative.

At the time, there were two other lightweight Web servers that were widely focused on ———— lighttpd and Boa to address the c10k problem, but their performance was hard to reach Nginx.

Nginx's Reverse proxy

One of the important reasons Nginx can stand out in many lightweight servers is that it has a very strong reverse proxy capability.

Different from the general small and medium Web site server directly accept user requests, and then run the corresponding PHP, Java and other program code, reverse proxy is the reverse proxy server as a receiver to accept the request of the terminal, and then in the next side in the configuration of a number of truly service Web application server.

A reverse proxy server typically distributes tasks based on the requested task type and the load of the alternate logical server (load Balancing).

In addition to Nginx support HTTPS SSL/TLS protocol, you can not have this function of the application to add HTTPS support (user and reverse proxy server through the encryption of HTTPS to communicate, the reverse proxy server through a secure intranet directly and the logical server communication).

At the same time, the reverse proxy server can cache a variety of static resources (such as picture files and HTML files), which makes it very good at processing a large number of duplicate requests.

In addition to the most mainstream Http/https mentioned above, Nginx also handles load balancing for protocols such as SMTP, IMAP, and POP3. It can even soon support the "Next generation HTTP"--spdy protocol.

talk about the expansion of the module

Because Nginx is a lightweight Web server, in addition to the core functions of reverse proxy and resource caching, avoid adding unnecessary functional modules as much as possible.

Of course, Nginx has many excellent standard libraries and third-party modules, such as traffic and connection monitoring and restriction, image format conversion and so on.

However, if you want to add a new module, you must recompile the entire program. Although this is not as convenient as the dynamic loading of Apache HTTP, it can guarantee the maximization of running efficiency and the minimization of resource consumption.

Nginx generally have a variety of installation packages, which comes with different non-core modules, must be known before installation.

How to handle dynamic resources

Nginx is good at load balancing and processing static resources such as HTML files and pictures, but it is not possible to dynamically generate resources on its own except for SSI (Server Side Include) technology.

At this point, we can actually use the UNIX Domain socket to let other processes process and return the corresponding dynamic resources. If the process is on another machine, it can be communicated over TCP.

Specifically, you can use FastCGIfor communication between PHP programs, Uwsgifor Python, and phusion passengerfor Ruby on Rails.

High-performance Tips ———— event-driven

Nginx is able to handle a large number of requests at the same time because it employs a very ingenious event-driven mechanism.

As a Web server, to handle multiple requests at the same time, it is inevitable to face the problem of how to handle I/O requests like disks and networks, and how I/O is reused.

To solve this problem, the operating system began to provide system calls such as "select", "poll", and so on for a long time. These system calls are used by the Apache HTTP multi-processing module (mpm,multi-processing module).

However, to identify which files or sockets are ready, Select/poll must check all registered file descriptors (FD) one at a time. The longer the registration list is, the longer it takes to scan each time.

The nginx I/O multiplexing mechanism uses "Epoll", an event-driven system call. Because Epoll manages and listens to these file descriptors (FD) in the system kernel and automatically joins the ready into the ready queue.

Therefore, the program only needs to ready queue to take out a processing can be, instead of switching to the kernel state, and then check each, and then switch back to user state.

This will not affect the efficiency of the program, regardless of how much I/O is required to register for listening.

To avoid the linear growth burden of select/poll, Apache http must scatter these IO processes/threads, which is bound to result in memory growth.

However, Nginx can use "epoll" to ensure that all requests can be processed using a single process/thread, which can greatly reduce memory footprint, making it possible to respond to tens of thousands of concurrent requests.

Apache VS Nginx

The horizontal axis represents the number of concurrent connections



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.