Nginx/OpenResty 指令的执行顺序
- http, 可以通过 init_by_lua 加载公共函数,比如
lua-resty-core
. - server selection,listen,server_name.
- post read, ngx_realip.
- server rewrite, set, rewrite, return, set_by_lua.
- server rewrite tail, rewrite_by_lua.
- server access, allow, deny.
- server access tail, access_by_lua.
- server try_files.
- location:
- prefix strings 遵循 最长子串匹配原则
- regular expressions 遵循 先定义优先匹配原则
location = {exact_url}
精准匹配location ~ {case-sensitive regex}
区分大小写location ~* {case-insensitive regex}
不区分大小写location ^~ {prefix_string_if_any}
一旦字符匹配成功,就不再正则匹配- 尽量不要
if
,换用try_files
-f
检测文件是否存在,-d
目录,-e
文件/目录/符号链接,-x
可执行文件
- location rewrite, set, rewrite, return, set_by_lua.
- location rewrite tail, rewrite_by_lua.
- preaccess, degradation, limit_zone, limit_req, ngx_realip.
- location access, allow, deny, auth_basic.
- location access tail, access_by_lua.
- content, ngx_echo, proxy_pass, content_by_lua.
- 请求具体处理阶段,只能有一个 内容处理程序(content handler)
- 多个 echo 可以共存,因为同属于 ngx_echo 模块,但 ngx_lua 限制只能有一个 content_by_lua.
- ngx_echo 的 echo_before_body/echo_after_body 可以和其他模块共存
- 如果没有 ngx_echo, proxy_pass, content_lua 这些 content handler,Nginx 会根据 URL 将请求映射到静态资源服务模块,依次是 ngx_index, ngx_autoindex, ngx_static.
- ngx_index/ngx_autoindex 处理以
/
结尾的请求,ngx_static 正好相反。
- output header filter, more_set_headers 输出 Headers.
- output filter echo_before_body, echo_after_body, body_filter_by_lua.
- log, access_log, error_log, log_by_lua.
- post action.
Was this page helpful?