学习一些小指令
stub_status on;监听 nginx 客户端的状态stub_filter "old-content" "new-content";将旧的内容替换为新的内容ab -n 总共的请求数 -c 并发的请求数 url可以测试 Web 服务器的压力
stub_status on; 监听 nginx 客户端的状态
开始步骤
- cd /usr/local/etc/nginx
- vi nginx.conf
server {
listen 8081;
server_name localhost;
# 如下表示: 访问 localhost:8081/status 将会返回 服务的 状态
location = /status {
stub_status on; # 开启服务器状态监听
}
}
- 浏览器输入 localhost:8081/status
stub_status 开启后返回的内容解析
Active connections: 2 # 当前 nginx 正在处理的活动连接数
server accepts handled requests # accepts=总共处理的连接数 handled=成功创建的握手数 requests=总共处理请求数
5 5 4
Reading: 0 Writing: 1 Waiting: 1
参数解析
| 参数 | 含义 |
|---|---|
| Active connnections | 当前 nginx 正在处理的活动连接数 |
| accepts | 总共处理的连接数 |
| handled | 成功创建的握手数 |
| requests | 总共处理的请求数 |
| Reading | 读取到客户端的 Header 信息数 |
| Writing | 返回给客户端的 Header 信息数 |
| Waiting | 开启 keep-alive 的情况下waiting = active - ( reading + writing ); |
stub_filter "old-content" "new-content"; 将旧的内容替换为新的内容
开始步骤
- cd /usr/local/etc/nginx
- vi nginx.conf
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
# sub_filter "old" "new"; 将返回内容"old"的替换成"new"
# 会将 index.html 中的 world 单词替换为 universe 单词
sub_filter "world" "universe";
# 关闭一次只替换一个的问题
sub_filter_once off;
}
}
- brew services restart nginx
- 访问 http://localhost:8081/
⚠️: 如果没有更新内容,可能是资源走了缓存,记得关闭缓存
ab 测试 web 服务器的压力
Apache 的 ab 命令模拟多线程并发请求, 测试服务器负载压力,也可以测试 nginx
# 测试 http://localhost:8081/ 总共请求 40 个,并发请求 20 个的压力
ab -n 40 -c 20 http://localhost:8081/
得到的结果大概为:
Server Software: nginx/1.23.3 # 服务软件
Server Hostname: localhost # 服务主机名
Server Port: 8081 # 服务端口
Document Path: / # 文档路径 /
Document Length: 679 bytes
Concurrency Level: 20 # 并发水平
Time taken for tests: 0.006 seconds # 测试花费的时间
Complete requests: 40 # 完成的请求数
Failed requests: 0 # 失败的请求数
.....
limit_req_zone 请求限制
limit_req_zone 定义限制请求区域
# 可以以 IP 为 key; zone 为空间的名称; size 为申请空间的大小
# 在 大小为 zoneName 的区域限制 key 的 rate为多少
syntax: limit_req_zone key zone=zoneName:zoneSize rate=rate(一般为 1r/s r表示request, 1r/s 表示频率为 1s 一个 请求)
Context: http(定义在 server 模块以外)
limit_req 限制请求
# zone 名称 number 限制的数量
Syntax: limit_req zone=name [burst=number] [nodelay]
Context: http, server, location
开始步骤
- cd /usr/local/etc/nginx
- vi nginx.conf
http {
# 定义一个限制区域: 限制区域名字叫做 req_zone(其空间为10兆)
# 这个限制区间 针对的 是 $binary_remote_addr,让其请求在 1 s只能请求一次
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html index.htm;
# 使用 http 模块定义的 req_zone 来限制但前 server 下的请求
limit_req zone=req_zone;
# limit_req zone=req_zone burst=3 nodelay
# 缓存队列 brust=3 个,不延迟,每秒最多可处理 rate+burst个,同时处理 rate 个
}
}
}
说明:
limit_req zone=req_zone burst=3 nodelay
- zone=req_zone 表示这个参数对应的全局设置就是 req_zone 的那个内存区域
- zone=req_zone: 10m 表示一个内存区域大小为10m,并且设置了区域名称为 req_zone
- $binary_remote_addr 表示远程的地址
- rate=1r/s 表示请求的速率是 1 秒 1 个请求
- burst=3 表示请求队列的长度
- nodelay 表示不延迟
- sudo brew services restart nginx
- ab -n 40 -c 20 http://localhost:8081/ 查看结果
Concurrency Level: 20 # 并发量为 20
Time taken for tests: 0.005 seconds
Complete requests: 40 # 完成的请求数
Failed requests: 39 # 失败的请求数; 说明成功的请求数为 40-39 = 1
limit_conn_zone 连接限制
limit_conn_zone 定义连接区域限制
# 定义 针对 某个 key 的连接区域限制的大小
Syntax: limit_conn_zone key zone=zoneName:size;
Context: http (server 以外)
limit_conn
Syntax: limit_conn zoneName limitNumber;
Context: http,server,location
开始步骤
- cd /usr/local/etc/nginx
- vi nginx.conf
http {
# 表明 以 ip 为 key,来限制每个 ip 访问文件的时候,最多只能有一个在线,否则其余都不可用
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
server {
location / {
root html;
index index.html index.htm;
limit_conn conn_zone 1;
}
}
}
http_access_module 访问控制
- http_access_module 基于 IP 的访问控制
- http_auth_basic_module 基于用户的信任登录
http_access_module
Syntax: allow address | all;
Context: http,server,location,limit_except;
# CIDR 就是 127.0.0.1/24 这样的,表示对应的 ip 与子网掩码 24 位相 与后 主机号相同即可互相访问
Syntax: deny address | CIDR | all;
Context: http,server,location,limit_except;
server {
# location 相当于 url,访问 以 admin.html 开始的时候会命中此 location
location ~ ^/admin.html {
# 如下的配置信息即 处理 127.0.0.1 不能访问外,其他都允许访问
deny 127.0.0.1;
allow all;
}
}
注意: 需要在对应的 html 文件夹中创建 admin.html 文件才行
curl http://localhost:8081/admin.html, 会得到: curl: (7) Failed to connect to localhost port 80 after 8 ms: Connection refused
浏览器最终效果:
http_auth_basic_module (暂时不行)
Syntax: auth_basic string|off;
Default: auth_basic off;
conext: http,server,location,limit_except;
Syntax: auth_basic_user_file file;
Default: -;
Context: http, server,location, limit_except
# htpasswd -c file userName # 往 file 中写入 userName 账号
# vi nginx.conf
http {
# 定义基本的校验信息内容,如果没有权限则显示此信息
auth_basic "must login brefore visiting the site.";
# 定义用户文件,这里的 custom_nginx_folder 没写逻辑和 nginx -V 中的 --prefix 会进行拼接
auth_basic_user_file custom_nginx_folder/user.conf;
}