nginx 初探一

首先参考一下 emiller的模块开发介绍这篇

http://www.evanmiller.org/nginx-modules-guide.html



Nginx模块有三个主要的角色

Handler, 处理请求,并产生结果
Filter, 过滤结果
Load-balancer 负载均衡

Note: Unlike modules in Apache, Nginx modules are not dynamically linked. (In other words, they're compiled right into the Nginx binary.)

换句话说,nginx用集成的方式,apache用加载的方式;必然导致nginx相对简单

Nginx在启动后,任何一个handler都会尝试加载配置文件,但好的配置文件只会让一个成功;而且解析结果,或者成功,或者错误,或者默认

Ngixn作为反向代理服务器时,主要起负载均衡的作用。目前有两种负载均衡的算法,一个是随机,一个是IP映射

handler处理成功后,就轮到了filter。采用责任链的设计模式,即一个filter成功后,去调用下一个,直到结束

Filter 链酷的地方在于它采用了类似于unix管道的模式,下面的filter不用等到上一个filter完全处理完,就可以开始处理那些存在buffer里的处理好的结果了。buffer的大小默认为一个页面大小,即4k

一个典型的nginx请求流程如下
Client sends HTTP request → Nginx chooses the appropriate handler based on the location config → (if applicable) load-balancer picks a backend server → Handler does its thing and passes each output buffer to the first filter → First filter passes the output to the second filter → second to third → third to fourth → etc. → Final response sent to client

nginx的个性化体现在下面的地方,这些地方,你可以增加个性的处理

Namely, you can provide a function to be executed:

  • Just before the server reads the config file 读配置文件之前
  • For every configuration directive for the location and server for which it appears; 每个不同位置和server处理
  • When Nginx initializes the main configuration 初始化主配置时
  • When Nginx initializes the server (i.e., host/port) configuration 初始化server配置时
  • When Nginx merges the server configuration with the main configuration 合并主配置和server的配置时
  • When Nginx initializes the location configuration 初始化位置配置
  • When Nginx merges the location configuration with its parent server configuration 合并位置配置和父server配置时
  • When Nginx's master process starts 主进程启动时
  • When a new worker process starts 工作进程启动时
  • When a worker process exits 工作进程退出时
  • When the master exits 主进程退出时
  • Handling a request 处理请求时
  • Filtering response headers 过滤请求包头时
  • Filtering the response body 过滤请求包体时
  • Picking a backend server 选择后备server时
  • Initiating a request to a backend server 初始化请求给后备server时
  • Re-initiating a request to a backend server 重新初始化请求给后备server时
  • Processing the response from a backend server 处理从后备server返回的结果时
  • Finishing an interaction with a backend server 和后备server完成交互时

Module由三种配置结构体来定义, main, server, location contexts。大多数Module只需要location configuration。 命名惯例如下
Ngx-http-<module name>-(main|srv|loc)-conf-t (此处-表示下划线), 例如
dav模块
typedef struct {
    ngx_uint_t  methods;
    ngx_flag_t  create_full_put_path;
    ngx_uint_t  access;
} ngx_http_dav_loc_conf_t;



nginx 代码的目录结构 参考http://code.google.com/p/nginxsrp/wiki/NginxCodeReview

解开nginx的代码后,在src目录下发现有如下的几个目录

core  event  http  mail  misc  os

其中 :

  • core : 该目录存放core module的代码,也是nginx服务的入口
  • http : http core module 的代码,nginx作为web/http proxy server运行时的核心模块
  • mail : mail core module 的代码,nginx作为pop3/imap/smtp proxy server运行时的核心模块 ( 不在我们本次研究范围内 )
  • event : nginx 自身对事件处理逻辑的封装
  • os : nginx对各个平台抽象逻辑的封装
  • misc : nginx 的一些utils,定义了test和profiler的一些外围模块的逻辑


原文地址:https://www.cnblogs.com/dyllove98/p/3131123.html