好架构,就是你要有预见性。比如 TCP 协议,它在 1995 年的时候就创立出来了,到现在都没有大改,依旧服务了各种各样的场景。Nginx 也是一样,从 2014 年推出来后基本没多大变化。但它的模块化,是纵向的划分的架构,最底层是事件驱动,基于 EP 的事件驱动,再上面是 HTTP 框架,再上面是 HTTP 模块,再上面是 OpenResty 的 lua。再看横向的划分,比如说我们要做一些 WAF 防火墙的限制,又比如基于 IP 做白名单、黑名单,那它的模块划分很清楚,还比如其他的模块,像 linit 或者 Request 只负责限速,不会自己去获取到用户的真实 IP 再去限速等等。
所以,我说 Nginx 是一个好的架构,而我们学习 Nginx,也可以获取到非常好的架构思维。
Nginx出现在哪里
执行流程,module的功能在特定的流程处理阶段得到执行,和conf里面的配置顺序没有关系
POST_READ The ngx_http_realip_module registers its handler at this phase to enable substitution of client addresses before any other module is invoked
SERVER_REWRITE Rewrite directives defined in a server block (but outside a location block) are processed
FIND_CONFIG Special phase where a location is chosen based on the request URI
REWRITE For rewrite rules defined in the location, chosen in the FIND_CONFIG phase
POST_REWRITE Special phase where the request is redirected to a new location if its URI changed during a rewrite
PREACCESS Common phase for different types of handlers, not associated with access control. The standard nginx modules ngx_http_limit_conn_module and ngx_http_limit_req_module register their handlers at this phase
ACCESS Phase where it is verified that the client is authorized to make the request. Standard nginx modules such as ngx_http_access_module and ngx_http_auth_basic_module register their handlers at this phase
POST_ACCESS Special phase where the satisfy any directive is processed. If some access phase handlers denied access and none explicitly allowed it, the request is finalized. No additional handlers can be registered at this phase
CONTENT Response is normally generated. Multiple nginx standard modules register their handlers at this phase, including ngx_http_index_module or ngx_http_static_module
LOG Request logging is performed. Currently, only the ngx_http_log_module registers its handler at this stage for access logging. Log phase handlers are called at the very end of request processing, right before freeing the request
Modules
realip[POST_READ PHASE]
break 停止执行rewrite模块中的指令集,影响其他模块的指令执行。
rewrite
break 停止执行rewrite模块中的指令集,影响其他模块的指令执行
if 变量,正则表达式,文件,路径,是否可执行,和shell中有点类似
return code [text]; return code URL; return URL
301 permanent may incorrectly sometimes be changed to a GET method
302 temporary some old clients were incorrectly changing the method to GET: the behavior with non-GET methods and 302 is then unpredictable on the Web, whereas the behavior with 307 is predictable. For GET requests, their behavior is identical
303 temporary This response code is usually sent back as a result of PUTor POST. The method used to display this redirected page is always GET
307 temporary guarantees that the method and the body will not be changed
308 permanent request method and the body will not be altered
nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered.
Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used
If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used