Nginx and architecture design

1.1.  What is Nginx

Nginx Russians prepared very lightweight HTTP servers , Nginx , it's pronounced "the X-Engine" , it is a high-performance HTTP server and reverse proxy, but also a IMAP / POP3 / SMTP proxy server.

Nginx because of its stability, rich library of modules, flexible configuration and low system resource consumption is known. The industry agreed that it is Apache2.2 + mod_proxy_balancer a lightweight alternative to, not only because of the static page response speed is very fast, and it's the number of modules to achieve the Apache nearly two-thirds . Of proxy and rewrite module support very thorough, we support mod_fcgi , SSL , vhosts , suitable for applications mongrel clusters front end of the HTTP response.

Currently Nginx in many large domestic enterprises have applied, and the penetration rate increased year by year. Select Nginx reason is simple:

First, it can support 5W high concurrent connections;

A second, less memory consumption;

Third, low cost.

1.2.  Nginx play a role in the architecture

  • Gateway 

--- total customer-facing entrance. 

  • Web Hosting 

--- a machine to a different domain name / ip / port to provide services 

  • routing 

--- use a reverse proxy, follow-up services for the integration of a complete business 

  • Static server 

--- mvvm mode, used to publish front-end html / css / js / img

  • Cluster Load 

--- use upstream, load multiple tomcat

 

 Nginx architecture design

2.1.  Nginx modular design

Highly modular design is Nginx architecture foundation. Nginx server is broken down into a number of modules, each module is a functional module, is only responsible for its own function, strictly follow the module between " high cohesion, low coupling " principle.

 

 

  • Core Module

The core module is Nginx server uptime essential modules that provide error logging configuration file parsing, event-driven mechanism, process management and other core functions.

  • Standard HTTP module

Standard HTTP module providing HTTP protocol resolution-related functions, such as: port configuration, encoding settings page, HTTP response header and the like.

  • Optional HTTP module

Optional HTTP module is mainly used to extend the standard HTTP features that make Nginx can handle some special services, such as: Flash multimedia transmission, analytical GeoIP request, SSL support.

  • Mail service module

Mail service module is mainly used to support Nginx mail services, including POP3 protocol, IMAP protocol and SMTP support agreement.

  • Third party modules

Third-party modules to extend Nginx server applications, developer complete custom features, such as: Json support, Lua support.

 

2.2.  Nginx multi-process model 

 

 

2.1、服务器每当收到一个客户端时。就有服务器主进程(master process)生成一个子进程(worker  process)出来和客户端建立连接进行交互,直到连接断开,该子进程结束。

2.2、使用进程的好处是各个进程之间相互独立,不需要加锁,减少了使用锁对性能造成影响,同时降低编程的复杂度,降低开发成本。

其次,采用独立的进程,可以让进程互相之间不会影响,如果一个进程发生异常退出时,其它进程正常工作,master 进程则很快启动新的 worker 进程,确保服务不中断,将风险降到最低。

缺点是操作系统生成一个子进程需要进行内存复制等操作,在资源和时间上会产生一定的开销;当有大量请求时,会导致系统性能下降。

 

2.3. Nginx的epoll模式

 

 

select和poll的处理模式如上图:

--在某一时刻,进程收集所有的连接,其实这100万连接中大部分是没有事件发生的。因此,如果每次收集事件时,都把这100万连接的套接字传给操作系统(这首先就是用户态内存到内核内存的大量复制),而由操作系统内核寻找这些链接上没有处理的事件,将会是巨大的浪费。

epoll改进了收集连接的动作,提高效率。

epoll的优点:

² 支持一个进程打开大数目的socket描述符(FD)

² IO效率不随FD数目增加而线性下降

² 使用mmap加速内核与用户空间的消息传递

2.4. 正向代理与反向代理

4.1、代理:意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。

 

 

4.2、反向代理,服务端推出的一个代理招牌。

 

 

Guess you like

Origin www.cnblogs.com/Soy-technology/p/11355597.html