Nginx 访问控制与变量
Nginx 访问控制与内置变量
limit_conn 并发连接限制
limit_conn 用于控制并发的连接数量,当并发连接请求超过设置数量会返回响应的状态码和提示信息
limit_conn_zone key zone=name:size
1 |
|
limit_conn_status <code>
定义限制触发后的状态码响应
limit_conn_level info | warn | debug | error
设置限制发生后的日志记录级别
limit_conn addr number
限制并发个数
limit_rate
响应数据的传输速率
1 |
|
limit_req 请求速率限制
limit_req_zone key zone=name:size rate=rate
1 |
|
limit_req_status code
定义限制触发后的状态码响应
limit_req_level info | notice | warn | error
limit_req zone=name [ burst=number ] [ nodelay | delay ];
1 |
|
allow/deny 访问控制列表
控制接收或者拒绝的ip 地址接入请求
控制权限由上到下生效
1 |
|
auth_basic
auth_basic string | off (默认)
auth_basic_user_file 存储鉴权文件路径
鉴权文件生成:
1 |
|
配置
1 |
|
auth_request
auth_request uri | off ;
重定向到别的模块
auth_request_set $variable value ;
1 |
|
rewrite
return
停止处理请求,直接返回响应码 或 重定向到其他URL
执行return后, location中后续指令将不再被执行
1 |
|
Rewrite
1 |
|
Return 和 Rewrite 的执行顺序
在server中的location 是一个循环查询过程。
所以 整个过程为http 处理 连接请求,匹配连接请求中的路径在server中进行查询
当 碰到 break 后, 中断本次查询拿着最新替换的 url在location 列表中继续匹配 (类似于编程语言中的continue)
当 碰到 last 后 , 中断本次查询 进入后续的文件查询步骤 (类似于编程语言中的break)
当 碰到 return 后 ,中断查询,使用return中提供的响应信息返回。
If
1 |
|
autoindex
用户请求以/结尾时,列出目录结构
autoindex
autoindex_exact_size
autoindex_format
autoindex_localtime
1 |
|
Nginx 变量
TCP连接变量
remote_addr 客户端ip地址
remote_port 客户端端口
server_addr 服务端ip地址
server_port 服务端端口
server_protocol 服务端协议
binary_remote_addr 二进制格式的客户端ip地址
connection TCP连接序号
connection_request TCP连接当前的请求数量
proxy_protocol_addr 代理的ip地址信息
proxy_protocol_port 代理的端口信息
Http请求变量
uri 请求URL, 不包含参数
request_uri 请求URL,包含参数
scheme 协议名,http或https
request_method 请求方法
request_length 全部请求的长度
args 全部参数字符串
arg_参数名 特定参数值
is_args URL中有参数,则返回? 否则返回空
query_string 与args相同
remote_user 由HTTP Basic Authentication 协议传入的用户名
host 主机名查询,多位置查询
http_user_agent 用户的浏览器代理
http_referer 从哪个链接进行的请求
http_via 中间经过的代理服务器路径
http_x_forwarded_for 经过代理后携带的真实用户ip信息
http_cookie 用户cookie
处理请求时产生的变量
request_time 处理请求已耗费的时间
request_completion 请求处理完成返回OK
server_name 匹配请求的server name值
https 开启https,则返回on , 否则返回空
request_filename 磁盘文件系统待访问路径
document_root 由URL和root/alias规则生成的路径
realpath_root 将document_root中的软链接转换为真实路径
limit_rate 响应时的速度上限值
响应变量
status 响应状态码
sent_http_content_type 响应头中的Content-Type
sent_http_cache_control 响应头中的Cache-Length
sent_http_* 任意发送的响应头字段
body_bytes_sent 已发送的响应body字节数
bytes_sent 已发送的总字节数