配置冲突

典型结构:

image.png

http -> server -> location

指令的上下文

image.png

指令合并

image.png

值指令, 向上覆盖:

image.png

接收请求事件模块

image.png

  • 请求内存池
  • 连接内存池
  • ngx_http_wait… 从 connection_pool_size(连接内存池) 中获得内存

可以这么理解连接/请求内存池, 它们都是某种结构体, 其中存储了各种状态数据.

接收请求 HTTP 模块

image.png

  • 分配大内存会拷贝原数据到新内存
  • 标识 URL 是指创建该 request url 的引用, 提高性能

  • 正则表达式

image.png

  • 测试正则表达式的工具 pcretest

server_name 指令

image.png

  • return 指令
  • server_name_in_redirect

image.png

Server 匹配顺序

image.png

HTTP 请求的11个处理阶段

image.png

post_read 获取一些原始值

  • 读取 header 之后, 加工之前

server_rewrite 只有 nginx 框架在处理

  • 只有 rewrite 模块

find config 只有 nginx 框架在处理

  • location 匹配

rewrite 只有 nginx 框架在处理

  • 只有 rewrite 模块

post_rewrite 刚刚 rewrite 之后做一些事情

  • 官方也没有在此阶段处理的模块

preaccess 确认访问权限有三个
access
post access

precontent 将请求发向第三方
content

log

image.png

处理顺序

image.png

  • 某个模块执行完, 可以跳过若干个模块, 再向后传递 req

postread

拿到用户真实 IP

image.png

  • x-forwarded-for 可以添加多个 ip
  • x-real-ip 只能添加一个

使用真实用户 ip

  • realip 模块会从 x-forwarded-for x-real-ip 中获得的值覆盖 binary_remote_addr remote_addr 变量

realip 模块

image.png

image.png

  • set_real_ip_from

定义受信的机器 ip, 从这些机器发来的 x-forwarded-for 中获取客户端真实地址

  • real_ip_header

从哪取客户端真实 ip

  • real_ip_recursive

排除 x-forwarded-fro 中与客户端相同的 ip

rewrite 模块

该模块生效后, 其它后面的阶段无法执行.

return:

image.png

  • 301 浏览器会缓存
  • 444 浏览器收不到该错误码

error_page:

image.png

rewrite:

image.png

rewrite_log 指令

if

image.png

image.png

find_config

location 指令:

image.png

image.png

preaccess

  • limit_req 限制每秒请求数
  • limit_conn 限制每秒连接数

leaky buctek 算法

image.png

access

allow 模块

allow 指令

image.png

auth_basic 模块

auth_request 模块

satisfy 指令

precontent

try_files 模块

image.png

mirror 模块

image.png

content

static 模块?

  • root
  • alias

image.png

提供的变量

image.png

静态文件返回的 content-type

image.png

响应中, 设置重定向 Location heaer

image.png

index 模块

autoindex 模块

concat 模块

image.png

log

log 模块

image.png

image.png

过滤模块

image.png

加工响应内容

image.png

变量

image.png

  • 惰性求值
  • 变量值可以时刻变化, 其值为使用的那一时刻的值

存放变量的哈希表

image.png

变量分类

image.png

常用变量

image.png

image.png

image.png

image.png

image.png

tcp 相关变量

image.png

image.png

处理请求所产生的变量

image.png

image.png

发送 http 响应所产生的变量

image.png

nginx 系统变量

image.png

referer 模块

  • 防盗链

secure_link 模块

  • 哈希

map 模块

split_client 模块

  • 实现ab测试

image.png

geo 模块

  • 根据 ip 地址生成变量

image.png

geoip

  • 计算 ip 所属地域信息
  • MaxMind 数据库

image.png

keepalive

image.png