Nginx 访问控制与变量

Nginx 访问控制与内置变量

limit_conn 并发连接限制

limit_conn 用于控制并发的连接数量,当并发连接请求超过设置数量会返回响应的状态码和提示信息

limit_conn_zone key zone=name:size

1
limit_conn_zone $binary_remote_addr zone=addr:10m 

limit_conn_status <code>

定义限制触发后的状态码响应

limit_conn_level info | warn | debug | error

设置限制发生后的日志记录级别

limit_conn addr number

限制并发个数

limit_rate

响应数据的传输速率

1
2
3
4
5
6
7
8
9
10
11
12
http {
...
limit_conn_zone $binary_remote_addr zone = addr : 10 m
location / {
root root_path ;
index index.html ;
limit_conn_status_code 503 ; # 限制触发时,返回503状态码
limit_conn_level warn ; # 设置log级别为warn
limit_conn addr 3 ; # 并发连接数为3
limit_rate 50; # 每秒传输50字节
}
}

limit_req 请求速率限制

limit_req_zone key zone=name:size rate=rate

1
2
limit_req_zone $binary_remote_addr zone=addr:10m rate=2r/m
# 限制每分钟处理两个请求 (平均处理速率 30s 处理一个)

limit_req_status code

定义限制触发后的状态码响应

limit_req_level info | notice | warn | error

limit_req zone=name [ burst=number ] [ nodelay | delay ];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# burst 表示允许接受突发请求
# nodelay 表示收到突发请求后直接返回响应
# delay 表示仍严格按照处理速率对请求进行处理

http {
...
limit_req_zone $binary_remote_addr zone=addr:10m rate=12r/m ;
location / {
root root_path ;
index index.html ;
limit_req_status 503 ; # 设置响应码为503
limit_req_level error ; # 设置日志级别为error
limit_req zone=addr burst=6 nodelay ; # 设置最大突发请求为6,不延迟
}
}

allow/deny 访问控制列表

控制接收或者拒绝的ip 地址接入请求

控制权限由上到下生效

1
2
3
4
location / {
allow 192.168.0.1 ;
deny all;
}

auth_basic

auth_basic string | off (默认)

auth_basic_user_file 存储鉴权文件路径

鉴权文件生成:

1
2
3
4
htpasswd
httdpd-tools
htpasswd -bc encrypt_pass jack 123456 # 创建鉴权文件
htpasswd -b encrypt_pass tom 123 # 增加鉴权信息

配置

1
2
3
4
5
6
location /auth {
root html ;
index index.html ;
auth_basic "check passwd"; # 设置提示信息
auth_basic_user_file pass_file_path ; # 设置鉴权文件路径
}

auth_request

auth_request uri | off ;

重定向到别的模块

auth_request_set $variable value ;

1
2
3
4
5
6
7
8
9
location /private/ {
auth_request /auth ; # 用户访问 /pirvate 后 跳转到/auth路径进行鉴权
}
location /auth {
proxy_pass <http://127.0.0.1:8080/verify> ; # 鉴权路径再次指向代理服务器验证路径
proxy_pass_request_body off ; # 设置向代理服务器发送的请求体为空
proxy_set_header Content-Length ""; # 设置向代理服务发送的请求长度为空
proxy_set_header X-Original-URI $request_uri ; # 设置原请求原uri信息
}

rewrite

return

停止处理请求,直接返回响应码 或 重定向到其他URL

执行return后, location中后续指令将不再被执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
return  code [text]  # 响应状态码 和 描述信息
...
location / {
return 200 "return 200 http status code ";
}
...
return code URL # 响应状态码 重定向到新的URL
...
location / {
return 302 /new_page ;
}
...
return URL # 重定向到新的URL , 必须是一个绝对路径的URL
...
location / {
return <http://ip>:port/new_page;
}
...

Rewrite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
rewrite regex replacement

例如 : rewrite /images/(.*\\.jpg)$ /pic/$1 ;

flag :
last : 重写后的URL发起新请求,再次进入server段,重新进行location的匹配
break : 直接使用重写后的URL,不再匹配location中的语句
redirect : 返回302临时重定向
permanent : 返回301永久重定向

server {
listen 80 ;
server_name info.codfish.cn ;
root html ;
location /serach {
rewrite ^/(.*) <http://www.codfish.cn> redirect|permanent # 匹配任意地址 重定向到目标地址
}
location /images{
rewrite /images/(.*) /pics/$1 break; # 匹配/images+ 任意字段 重定向到 /pics/$1 直接使用该URL请求
}
location /pics{
rewiret /pics/(.*) /photos/$1 last; # 替换为/photos/任意字段。再次依照此url发起匹配
}
location /photos{

}
}

Return 和 Rewrite 的执行顺序

在server中的location 是一个循环查询过程。

所以 整个过程为http 处理 连接请求,匹配连接请求中的路径在server中进行查询

当 碰到 break 后, 中断本次查询拿着最新替换的 url在location 列表中继续匹配 (类似于编程语言中的continue)

当 碰到 last 后 , 中断本次查询 进入后续的文件查询步骤 (类似于编程语言中的break)

当 碰到 return 后 ,中断查询,使用return中提供的响应信息返回。

If

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if ( condition ) {...} ;
# 依照表达式 匹配重定向路径

if ($http_user_agent ~ Chrome){
rewrite /(.*) /browser/$1 break ;
}

# 判断表达式
# $variable 非空为true,空为false
# = 或 != 相等/不等
# ~ 或 !~ 正则/非正则匹配
# ~* 正则不区分大小写
# -f 或 ! -f 文件/非文件
# -d 或 ! -d 存在/不存在
# -e 或 ! -e 检查文件,目录,链接是否存在
# -x 或 ! -x 检查文件可执行/不

autoindex

用户请求以/结尾时,列出目录结构

autoindex

autoindex_exact_size

autoindex_format

autoindex_localtime

1
2
3
4
autoindex  on | off   列出目录下的所有文件
autoindex_exact_size on | off 显示文件的精确大小
autoindex_format html 指定返回的目录结构
autoindex_localtime 文件时间格式

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 已发送的总字节数


Nginx 访问控制与变量
http://gadoid.io/2025/05/18/Nginx-访问控制与变量/
作者
Codfish
发布于
2025年5月18日
许可协议