您的位置:首页 > 运维架构 > Nginx

Nginx模块开发指南(Emiller) 二

2013-09-05 15:11 274 查看
If the handlerhappens to be a reverse proxy to some set of backend servers, there is room foranother type of module: the load-balancer. A load-balancer takes a request anda set of backend servers
and decides which server will get the request. Nginxships with two load-balancing modules: round-robin, which deals out requestslike cards at the start of a poker game, and the "IP hash" method,which ensures that a particular client will hit the same backend
server acrossmultiple requests.
如果handler碰巧是针对一些后台服务器的反向代理,那么它就变成了另一种类型的模块: load-balancer(译者注:负载均衡模块).一个load-balancer模块接受一个请求,并且拥有一系列的后台服务器,然后它决定那台服务器将获取到这个请求。Nginx load-balancer采用了两种策略实现负载均衡:轮询,它处理请求就行扑克牌发牌一样从头到尾依次分发;
IP哈希方法,它能确保特定的客户端请求到达相同的后端服务器。
If the handlerdoes not produce an error, the filters are called. Multiple filters can hookinto each location, so that (for example) a response can be compressed and thenchunked. The order of
their execution is determined at compile-time. Filtershave the classic "CHAIN OF RESPONSIBILITY" design pattern: one filteris called, does its work, and then calls the next filter, until the finalfilter is called, and Nginx finishes up the response.
假如handler没有产生错误,filters模块会被调用。多个Filter模块钩挂到相应的位置.举个例子,这样一个响应就能被压缩打包.
Filters执行的顺序是在编译的时候决定的。Filters是经典的“职责链”设置模式:一个Filter被调用,完成它的事情,然后调用下一个Filter,直到最后一个Filter被调用,Nginx完成了这次响应。
The really coolpart about the filter chain is that each filter doesn't wait for the previousfilter to finish; it can process the previous filter's output as it's beingproduced, sort of like
the Unix pipeline. Filters operate onbuffers,which are usually the size of a page (4K), although you can change this in yournginx.conf. This means, for example, a module can start compressing theresponse from a backend server and stream it to the
client before the modulehas received the entire response from the backend. Nice!
Filter模块链真正cool的地方在于:每一个Filter不用等前一个Filter全部完成就能把前一个Filter的输出作为处理内容进行处理;类似于Uninx系统中的管道.
Filter以Buffer(缓冲区)为单位进行操作, Buffer大小经常是4K,你也可以在nginx.conf配置文件更改Buffer大小.这种模式就意味着一个模块在接受到来自于后端服务服务器全部数据之前就可以开始压缩后端服务的相应数据并且把传输给客户端。Very
Good!
So to wrap up theconceptual overview, the typical processing cycle goes:
Client sends HTTP request→ Nginxchooses the appropriate handler based on the location config→
(if applicable) load-balancer picks abackend server→ Handler does its thing and passeseach output buffer to the first filter→
First filterpasses the output to the second filter→ second tothird→ third to fourth→
etc. → Final response sent to client
I say"typically" because Nginx's module invocation is extremely customizable.
Itplaces a big burden on module writers to define exactly how and when the moduleshould run (I happen to think too big a burden). Invocation is actuallyperformed through a series of callbacks, and there are a lot of them. Namely,you can provide a function
to be executed:
让我们整体上整理一下这些概念,典型的处理流程是这样的:
客户端发送http请求 -> Nginx基于配置文件中Location配置选择合适的Handler进行处理->(如果配置了)load-balancer模块选择一个后端服务器->Handler进行处理并且输出传递给第一个Filter模块->第一Filter模块传递输出到第二个Filer->第二个传递到第三个->第三个传递到第四个->
… ->最后的响应返回给客户端。
我之所以说这个是个典型的流程是因为Nginx模块调用是可以高度自定义的。
准确的定义这些模块在什么时间,以怎样的方式运行起来对模块的开发者来说是一个沉重的负担(我也认为这是一个不小的负担).调用是通过一系列的函数回调完成的,并且在模块中确实存在这样大量的回调。从名义上来说,你能提供一个函数在下面的情况下被执行:

Just before the server reads the config file
就在Nginx在读取配置文件时候
For every configuration directive for the location and server for which it appears;
当读取location和server每个一个配置指令时
When Nginx initializes the main configuration
当Nginx初始化Main配置项时
When Nginx initializes the server (i.e., host/port) configuration
当Nginx初始化Server(例如: host/port)配置项时
When Nginx merges the server configuration with the main configuration
当Nginx合并Main配置和Server配置项时
When Nginx initializes the location configuration
当Nginx初始化location配置项时
When Nginx merges the location configuration with its parent server configuration
当Nginx合并location配置和上级Server配置项时
When Nginx's master process starts
当Nginx master进程启动时
When a new worker process starts
当一个新的Worker进程启动时
When a worker process exits
当一个Worker进程退出时
When the master exits
当Master进程退出时
Handling a request
当正在处理一个请求时
Filtering response headers
当对响应的头部进行过滤时
Filtering the response body
当对响应的body进行过滤时
Picking a backend server
当选取一个后端服务器时
Initiating a request to a backend server
建立一个请求到一个后端的服务器时
Re-initiating a request to a backend server
重新建立一个请求到一个后台服务器时
Processing the response from a backend server
处理来自于一个后端服务器的响应时
Finishing an interaction with a backend server
当完成和后端服务器一次交互时

Holy mackerel!It's a bit overwhelming. You've got a lot of power at your disposal, but youcan still do something useful using only a couple of these hooks and a coupleof corresponding functions.
Time to dive into some modules.
天呀!这似乎有点不可控。根据你自己的安排你能获取很大权力,但是你仍可以仅通过一些hook函数和一些响应函数做一些有用的事情。让我们逐步来学习这些模块。

后续..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: