一:位置替换
1.1 alias
只能作用于location中
# 替换掉location定义的访问路径
# 图片资源在image/下,访问http:zsl.com/alias/1.jpg返回image/1.jpg图片
location /alias {
alias image;
}
# 如果location结尾定义了/,alias结尾必须用/结束
location /alias/ {
alias image/;
}
复制代码
1.2 root
可以作用于http、server、location、if in location
# 访问路径前增加一段根目录
# 图片在image/root/,访问http://zsl.com/root/1.jpg返回image/root/1.jpg
location /root {
root image;
}
复制代码
二:请求头与请求体
2.1 client_header_buffer_size
可以作用于http、server
# 定义客户端请求头缓冲区大小
# 默认值1K
# 如果超过设定值则会large_client_header_buffers参数分配
client_header_buffer_size 1k;
复制代码
2.2 large_client_header_buffers
可以作用于http、server
# 定义客户端大请求头缓冲数量以及大小
# 默认值有4个 8K大缓冲
# 这是临时分配缓冲内存,使用完毕将被回收
# number 表示开辟的数量 size表示开票的大小
# 每个请求只能占用一个large_client_header_buffers,超过返回414状态码
large_client_header_buffers number size
复制代码
2.3 client_header参数配置建议
- 频繁请求的请求头巨大,考虑设置修改client_header_buffer_size大小
- 偶尔请求的请求头巨大,考虑设置修改large_client_header_buffers。因为这是申请的临时内存,使用完以后会被回收,减少内存占用
- 不论多少server配置了client_header_buffer_size/large_client_header_buffers,最后这个参数值会选定default_server设定的值作为最终值
2.4 client_max_body_size
可以作用于http、server、location
# 请求体最大正文
# 默认值在64位平台上为512字节
# 如果超过这个配置则会返回413状态异常码
client_max_body_size 512k
复制代码
2.5 client_body_buffer_size
可以作用于http、server、location
# 请求体内存缓存大小
# 默认值在64位平台上为16k
# 超过设定值则会根据client_body_temp_path 参数写入临时文件
client_body_buffer_size 16k
复制代码
2.6 client_body_temp_path
可以作用于http、server、location
# client_body超过buffer设定值以后临时文件位置
# 默认位置为client_body_temp
client_body_temp_path client_body_temp
复制代码
2.7 client_body参数配置建议
为了尽量使用内存避免临时文件的IO读写浪费,最好是将client_body_buffer_size大小设置为与client_max_body_size一致
三:KeepAlive
3.1 keepalive_requests
可以作用于http、server、location
# 连接发出限定数量请求后关闭
# 可以有效清理连接占用内存
# 默认100,不建议设置过大
keepalive_requests 100
复制代码
3.2 keepalive_timeout
可以作用于http、server、location
# timeout表示连接闲置存活时间
# header_timeout表示在response响应头中增加Keep-Alive: timeout=
keepalive_timeout timeout [header_timeout]
复制代码
四:location配置
nginx对请求的处理落脚点还是在location上,uri通过规则与location进行匹配。location匹配路径配置方式如下,文中顺序为匹配优先级,即多个location匹配时需要根据优先级决定处理的location
4.1 规则匹配类型
匹配规则 | 规则描述 | 优先级别 |
---|---|---|
= | 全量匹配,uri必须与location规则一致 | 1 |
^~ | 前置匹配,uri从左开始前置内容完全与location规则一致 | 2 |
~ | 正则匹配,支持正则符号匹配 | 3 |
~* | 正则匹配,忽略大小写 | 4 |
/zsl | 前置匹配,与^~冲突互斥 | 5 |
/ | 任何匹配都会成功 | 6 |
五:default_type
可以作用于http、server、location
# 指定处理文件类型
default_type application/json;
复制代码
六:error_page
可以作用于http, server, location, if in location
# 处理指定错误状态码请求
# error_page code ... [=] uri;
# code 指定处理的错误状态码
# = 可以指定修改返回的状态码
# uri 处理该错误的uri重新进行location匹配
error_page 404 /error.html;
location = /error.html {
root html;
}
复制代码