Heim  >  Artikel  >  Datenbank  >  NGINX模块的功能

NGINX模块的功能

WBOY
WBOYOriginal
2016-06-07 15:03:531018Durchsuche

未完待续: 有几个阶段是特例,它不调用挂载地任何的handler,也就是你就不用挂载到这几个阶段了 NGX_HTTP_FIND_CONFIG_PHASE NGX_HTTP_POST_ACCESS_PHASE NGX_HTTP_POST_REWRITE_PHASE NGX_HTTP_TRY_FILES_PHASE 只有下面这几个phase会有多个handler,剩余的都

未完待续:


有几个阶段是特例,它不调用挂载地任何的handler,也就是你就不用挂载到这几个阶段了
NGX_HTTP_FIND_CONFIG_PHASE
NGX_HTTP_POST_ACCESS_PHASE
NGX_HTTP_POST_REWRITE_PHASE
NGX_HTTP_TRY_FILES_PHASE



只有下面这几个phase会有多个handler,剩余的都是只有一个handler的
NGX_HTTP_POST_READ_PHASE   
NGX_HTTP_SERVER_REWRITE_PHASE,  
NGX_HTTP_REWRITE_PHASE,  
NGX_HTTP_PREACCESS_PHASE,  
NGX_HTTP_ACCESS_PHASE,  
NGX_HTTP_CONTENT_PHASE,  
NGX_HTTP_LOG_PHASE


NGX_HTTP_POST_READ_PHASE

checker:
ngx_http_core_generic_phase
handler:
ngx_http_realip_handler



NGX_HTTP_SERVER_REWRITE_PHASE:
checker:
ngx_http_core_rewrite_phase    
handler:
ngx_http_rewrite_handler
sever区块中如果有包含rewrite规则,则会最先执行,而且只会执行一次, 然后再判断命中
哪个location的配置,再去执行该location中的rewrite, 当该location中的rewrite执行完
毕时,rewrite并不会停止,而是根据rewrite过的URL再次判断location并执行其中的配置. 
那么,这里就存在一个问题,如果rewrite写的不正确的话,是会在location区块间造成无限
循环的.所以nginx才会加一个最多重试10次的上限.

NGX_HTTP_FIND_CONFIG_PHASE:
ngx_http_core_find_config_phase

这是find config phase的checker,用于根据uri查找对应的location,nginx中location的处

理是相当复杂的,这里关注于phase handler的处理流程.find config phase只有一
个phase handler,并且它没有相应的handler回调函数,完成的只是根据uri匹配到location之
后,将location的loc_conf赋值给request,并且根据loc_conf对request进行一些处理.所有
的由静态字符串标识的location被称作static location,由正则表达式表示的location成
为regex location.所有的static location被组织成二叉树,以便于查找.在static location
匹配失败后,会进行regex location匹配.这个过程正好描述了nginx中location的匹配规则.

 find config的checker可能会被执行多次,当rewrite成功后,会修改uri参数,需要重新匹
 配location.这就是find_config_index存在的必要,它用于初始化post rewrite phase handler
 的next字段,当uri被修改后会重新跳至find config phase执行.



NGX_HTTP_REWRITE_PHASE:
checker:
ngx_http_core_rewrite_phase
handler:
ngx_http_rewrite_handler

sever区块中如果有包含rewrite规则,则会最先执行,而且只会执行一次, 然后再判断命中
哪个location的配置,再去执行该location中的rewrite, 当该location中的rewrite执行完
毕时,rewrite并不会停止,而是根据rewrite过的URL再次判断location并执行其中的配置. 
那么,这里就存在一个问题,如果rewrite写的不正确的话,是会在location区块间造成无限
循环的.所以nginx才会加一个最多重试10次的上限.




NGX_HTTP_POST_REWRITE_PHASE:
checker:
ngx_http_core_post_rewrite_phase
这是post rewrite phase的checker,用于对rewrite和server rewrite phase进行收尾工作.
request中有两个字段与重写相关:
uri_changed:uri是否被重写.
uri_changes:uri被重写的次数,初始值为11,所以只能重写10次.
server rewrite和rewrite的handler会修改这两个变量,实现重写.这个checker就是根据
uri_changed判断是否进入find config phase,然后再根据uri_changes做一些校验

NGX_HTTP_PREACCESS_PHASE:
checker:
ngx_http_core_generic_phase
handler:
ngx_http_degradation_handler
ngx_http_realip_handler
ngx_http_limit_req_handler
ngx_http_limit_conn_handler


NGX_HTTP_ACCESS_PHASE:
checker:
ngx_http_core_access_phase

handler:

ngx_http_access_handler

用于检查IP地址是否可以访问


ngx_http_auth_basic_handler

用于验证用户名和密码




NGX_HTTP_POST_ACCESS_PHASE:
checker:
ngx_http_core_post_access_phase

这个是post access phase的checker,用于对access phase做收尾处理. 在ngx_http_init_phase_handlers中只有当use_access为1时这个phase才会有handler, 也就是说只有在access phase注册了handler时这个phase才会添加到请求处理流程里.post access handler完成的工作很简单,就是根据access_code做些出继续处理下个phase还是终止请求


NGX_HTTP_TRY_FILES_PHASE:
checker:
ngx_http_core_try_files_phase

try_files 用于搜索指定目录下的N个文件,如果找不到fileN,则调用fallback中指定的位置来处理请求.

语法: try_files file1 [file2 ... filen] fallback
默认值: 无
作用域: location
这个指令的作用就是可接收多个路径作为参数,当前一个路径的资源无法找到,则自动查找下
一个路径,如当请求的静态资源不存在,就将请求fallback指定位置到后台服务器上进行动态处理.
简单的例子:
location / {
         try_files index.html index.htm @fallback;
       }
location @fallback {
         root /var/www/error;
         index index.html;
       }


NGX_HTTP_CONTENT_PHASE:
checker:
ngx_http_core_content_phase
handler:

ngx_http_random_index_handler

在uri指定的目录下随机找一个文件: 将目录下面的文件名都保存到一个数组中,然后从中随机选择一个文件名,将文件名拷贝到uri中


ngx_http_index_handler

根据index的配置,按顺序查找相应的index是否存在,查到就采用internal redirect


ngx_http_autoindex_handler
ngx_http_dav_handler
ngx_http_gzip_static_handler
ngx_http_static_handler


NGX_HTTP_LOG_PHASE:
checker:
ngx_http_core_generic_phase
handler:
ngx_http_log_handler
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn