飞跃高山与大洋的鱼飞跃高山与大洋的鱼
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
  • NGINX

    • 快速开启 Nginx
    • 为 Nginx 配置 systemd 服务
    • 处理 Nginx ERR
    • 使用 goaccess 可视化查看日志
    • Nginx 的限制模块
      • 请求限制
        • 1. limit_req_zone
        • 2. limit_req
      • 连接限制
        • 1. limit_conn_zone
        • 2. limit_zone
      • 访问 IP 限制
      • 访问 账号 限制
        • 1. 生成密码
        • 2. 限制说明
    • Nginx 的 location 规则
    • 主从 Nginx
    • Nginx 反向代理与负载均衡
    • Nginx 的 proxy_pass 规则
    • Nginx 防盗链
    • Nginx 的 rewrite 规则
    • root 与 alias 区别
    • Nginx 至 HTTPS
    • websocket 反向代理

Nginx 的限制模块

请求限制

请求限制 limit_req_module 比连接限制更优化,由于一个连接可以被多次复用。

1. limit_req_zone

首先需要在 http 作用域定义 limit_req_zone。

Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http

# Example
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

2. limit_req

再在指定的作用域启用。

Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location

# Example
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
location /search/ {
  limit_req zone=one burst=5;
}

limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;
server {
  limit_req zone=perip burst=5 nodelay;
  limit_req zone=perserver burst=10;
}

$binary_remote_addr 相对 remote_addr 占用的空间更少。

连接限制

limit_conn_module 步骤基本与请求限制一致。

1. limit_conn_zone

Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http

# Example
limit_conn_zone $binary_remote_addr zone=addr:10m;

2. limit_zone

Syntax: limit_zone name $variable size;
Default: —
Context: http

# Example
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;

server {
  limit_conn perip 10;
  limit_conn perserver 100;
}

访问 IP 限制

access_module 可用来限制访问的 IP,用法比较简单。

Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except

Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except

# Example
location / {
  deny  192.168.1.1;
  allow 192.168.1.0/24;
  allow 10.1.1.0/16;
  allow 2001:0db8::/32;
  deny  all;
}

访问 账号 限制

auth_basic_module 可以为路径添加用户密码访问,可配合 IP 限制使用。

1. 生成密码

# 生成密码工具 htpasswd
# -b 参数为 在一行输入用户名和密码,而非根据提示输入密码
# -c 参数为 创建一个加密文件(注意已有则覆盖),即增加用户时无需指定
htpasswd -bc /etc/nginx/auth_conf shanyuhai 123
# 若缺乏依赖则提示,安装即可
# centos 下
yum install httpd-tools
# ubuntu 下
apt install apache2-utils

cat /etc/nginx/auth_conf # 验证

2. 限制说明

Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except

Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except

# Example
location / {
  auth_basic           "请输入用户名、密码";
  auth_basic_user_file auth_conf;
}
编辑文档!
上次更新:
贡献者: shanyuhai123
Prev
使用 goaccess 可视化查看日志
Next
Nginx 的 location 规则