当前位置: 代码网 > 服务器>服务器>Linux > Nginx限流配置的几种方案的使用小结

Nginx限流配置的几种方案的使用小结

2025年05月21日 Linux 我要评论
nginx为我们提供了请求限制模块(ngx_http_limit_req_module)、基于令牌桶算法的流量限制模块(ngx_stream_limit_conn_module),可以方便的控制令牌速

nginx为我们提供了请求限制模块(ngx_http_limit_req_module)、基于令牌桶算法的流量限制模块(ngx_stream_limit_conn_module),可以方便的控制令牌速率,自定义调节限流,实现基本的限流控制

此模块已经合并至主线版本中,无需再额外编译添加

一、限制每个用户流量(并发限制)

nginx 并发限制的功能来自于 ngx_http_limit_conn_module 模块

模块的文档:module ngx_http_limit_conn_module

limit_conn_zone 只能配置在 http 范围内,可同时配置多条,被不同所引用;

$binary_remote_addr 表示客户端请求的ip地址;

one 自己定义的变量名(缓冲区);

size 设置为1m,大约为16000个ip地址(详细见文档)

limit_rate 限制传输速度

limit_conn 与 limit_conn_zone 对应,限制网络连接数

1、在http体添加配置说明

http
{
  limit_conn_zone $binary_remote_addr zone=one:1m; # 限速定义
}

2、在server体添加限速实现

server{
  limit_conn one 1; #限制每个ip只能发起一个并发连接
  limit_rate 256k;  #限制每个连接的限制速度为256k,ip的下载速度为连接数*限制速度
}

说明:为了减轻后端压力正常限制在接口层就可以

二、限制每个ip限定时间的访问次数(请求限制)

请求限制的功能来自于 ngx_http_limit_req_module 模块

模块文档:module ngx_http_limit_req_module

limit_req_zone 只能配置在 http 范围内;

$binary_remote_addr 表示客户端请求的ip地址;

mylimit 自己定义的变量名;

size 设置为1m,大约为16000个ip地址(详细见文档)

rate 请求频率,每秒允许多少请求;

limit_req 与 limit_req_zone 对应

burst 是配置超额处理,可简单理解为队列机制,让多余的请求可以先放到队列里,如果不加nodelay参数,队列里的请求不会立即处理,而是按照rate设置的速度,以毫秒级精确的速度慢慢处理

nodelay 参数允许请求在排队的时候就立即被处理,也就是说只要请求能够进入burst队列,就会立即被后台worker处理,请注意,这意味着burst设置了nodelay时,系统瞬间的请求可能会超过rate设置的阈值。nodelay参数要跟burst一起使用才有作用

1、在http体添加配置说明

http
{
  limit_req_zone $binary_remote_addr zone=mylimit:1m rate=5r/s; #限人数定义
}

2、在server体添加限速实现

server{
  limit_req zone=mylimit burst=100 nodelay;     #限人数实现
}

三、说明

1、整体限制就把限速实现放在sever层入口

2、限制接口访问次数就放在接口配置

参考:

http请求

http {
  include       mime.types;
  default_type  application/octet-stream;

  #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  #                  '$status $body_bytes_sent "$http_referer" '
  #                  '"$http_user_agent" "$http_x_forwarded_for"';

  #log_format  remote_main  '$remote_addr - $remote_user [$time_local] "$request" '
  #                  '$status $body_bytes_sent "$http_referer" '
  #                  '"$http_user_agent" "$http_x_forwarded_for" "$request_time" "$upstream_response_time" "$upstream_addr" "$upstream_status"';

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for" "$request_time" "$upstream_response_time" "$upstream_addr" "$upstream_status"';

  access_log  logs/access.log  main;

  sendfile        on;
  #tcp_nopush     on;

  proxy_buffers 16 1024k;
  proxy_buffer_size 1024k;

  gzip  on;

  keepalive_timeout  180s;
  proxy_connect_timeout 180s;
  proxy_send_timeout 180s;
  proxy_read_timeout 180s;

  server_tokens off;
  # add_header x-frame-options sameorigin;
  # add_header x-frame-options allow-from 'http://xxx.xxx.xxx.xxx:80';
  add_header x-content-type-options nosniff;
  add_header x-xss-protection "1; mode=block";
  add_header strict-transport-security "max-age=31536000; includesubdomains;";

  client_max_body_size 32m;
  client_body_buffer_size 256k;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 8k;

  absolute_redirect off;
  server_name_in_redirect off;
  port_in_redirect off;

  ### vts
  vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;

  # limit_req_zone $binary_remote_addr zone=test:10m rate=3r/s;
  include /opt/nginx/conf/conf.d/*.conf;


    #limit_conn_zone $binary_remote_addr zone=one:1m; # 限速定义
    #limit_req_zone $binary_remote_addr zone=mylimit:1m rate=5r/s; #限人数定义
}

server层配置

server {
  listen 80;
  listen 1023;
  listen 443 ssl;

  # limit_req zone=test burst=20 nodelay;
  #limit_conn_zone $binary_remote_addr zone=one:10m;
  #limit_conn one 2; #限制每个ip只能发起一个并发连接
  #limit_rate 256k; #限制每个连接的限制速度为256k,ip的下载速度为连接数*限制速度

  charset utf-8;
  # server_name lygyqgk.mylyg.cn 117.60.146.37;
  server_name lygyqfk.mylyg.cn 117.60.146.37 11.1.8.24;

  index index.html index.htm;
  ssl_certificate /opt/nginx/conf/cert/_.mylyg.cn.crt;
  ssl_certificate_key /opt/nginx/conf/cert/_.mylyg.cn.key;
  ssl_session_timeout 5m;
  ssl_ciphers "ecdhe-rsa-aes256-gcm-sha384:ecdhe-rsa-aes128-gcm-sha256:dhe-rsa-aes256-gcm-sha384:dhe-rsa-aes128-gcm-sha256:ecdhe-rsa-aes256-sha384:ecdhe-rsa-aes128-sha256:ecdhe-rsa-aes256-sha:ecdhe-rsa-aes128-sha:dhe-rsa-aes256-sha256:dhe-rsa-aes128-sha256:dhe-rsa-aes256-sha:dhe-rsa-aes128-sha:aes256-gcm-sha384:aes128-gcm-sha256:aes256-sha256:aes128-sha256:aes256-sha:aes128-sha:high:!anull:!enull:!export:!des:!md5:!psk:!rc4";
  ssl_protocols tlsv1 tlsv1.1 tlsv1.2;
  ssl_prefer_server_ciphers on;

  #压缩功能
  gzip_static on;
  gzip on;
  gzip_buffers 32 4k;
  gzip_comp_level 6;
  gzip_min_length 100;
  gzip_http_version 1.0;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png application/javascript;
  gzip_disable "msie [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
  gzip_vary on;

  gzip_disable "msie [1-6]\.(?!.*sv1)";
  gzip_disable "msie [1-6]\.(?!.*sv1)";



  # 测试中文
  # 请求体最大5m
  client_max_body_size 5m;
  # 根目录直接重定向到 /main
  location ~ ^/$ {
    return 301 /main/;
  }
  location /main {
    root  /usr/share/nginx/html;
    index index.html; 
    try_files $uri $uri/ /main/index.html;
  }
 
  location / {
    if ($request_filename ~* .*\.(?:htm|html)$)  ## åäöãò³ãæ²»»º´æhtmlºíhtm½áî²µäîä¼þ
      {
      add_header cache-control "no-cache";
      add_header access-control-allow-origin *;
    }
    root /usr/share/nginx/html/subapp;
    index index.html;
    try_files $uri $uri/ /index.html;
    add_header access-control-allow-origin *;
  }

  location /thirdapp{
    alias /usr/share/nginx/html/thirdapp;
  }
  location /cdn {
    add_header access-control-allow-origin *;
    add_header access-control-allow-headers x-requested-with;
    add_header access-control-allow-methods get,post,options;
    alias /usr/share/nginx/html/cdn;
    add_header  cache-control max-age=31536000;
  }

  location ^~ /api/ {
    # proxy_set_header host $host;
    proxy_set_header host $host:$server_port;
    proxy_set_header x-real-ip $remote_addr;  #¿¿¿¿¿¿¿ip
    proxy_set_header remote-host $remote_addr;
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
    proxy_pass http://api/;
    #限速实现 控制在接口层
    #         limit_conn one 1; #限制每个ip只能发起一个并发连接
    #         limit_rate 10k; #限制每个连接的限制速度为256k,ip的下载速度为连接数*限制速度
    #限人数实现
    #        limit_req zone=mylimit burst=100 nodelay;
  }
}
}

到此这篇关于nginx限流配置的几种方案的使用小结的文章就介绍到这了,更多相关nginx限流配置方案内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com