解读nginx中limit配置参数

 更新时间:2018年01月05日 14:12:13   投稿:laozhang  
这篇文章主要介绍了nginx中limit配置参数的详细作用,希望我们整理的内容能帮助到你,一起学习下吧。

本文主要解析一下ngx_http_core_module、ngx_http_limit_conn_module以及ngx_http_limit_req_module中的limit相关配置参数。

limit_rate

名称 默认配置 作用域 官方说明 中文解读 模块
limit_rate limit_rate 0; http, server, location, if in location Limits the rate of response transmission to a client. The rate is specified in bytes per second. The zero value disables rate limiting. The limit is set per a request, and so if a client simultaneously opens two connections, the overall rate will be twice as much as the specified limit. 指定每秒该连接能下载的bytes,主要用来限制个别请求的带宽 ngx_http_core_module
limit_rate_after limit_rate_after 0; http, server, location, if in location Sets the initial amount after which the further transmission of a response to a client will be rate limited. 设置多少bytes过后将启动limit计数,如果小于此值则不限速 ngx_http_core_module
limit_except 没有默认值 location Limits allowed HTTP methods inside a location. The method parameter can be one of the following: GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, or PATCH. Allowing the GET method makes the HEAD method also allowed 设置除了指定的http methods外其他method将被限制,允许GET就自动允许HEAD方法 ngx_http_core_module

实例

 location /downloads {
      limit_rate_after 1m;
      limit_rate 500k;
    }

    location / {
      proxy_pass http://localhost:3000;
      limit_except GET {
        deny all;
      }
    }

limit_conn

名称 默认配置 作用域 官方说明 中文解读 模块
limit_conn 没有默认值,语法 limit_conn zone number; http, server, location Sets the shared memory zone and the maximum allowed number of connections for a given key value. When this limit is exceeded, the server will return the error in reply to a request. 指定一个zone的每个key最大连接数 ngx_http_limit_conn_module
limit_conn_zone 没有默认值,语法 limit_conn_zone key zone=name:size; http Sets parameters for a shared memory zone that will keep states for various keys. In particular, the state includes the current number of connections. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted. 第一个参数是key,第二个参数是指定zone及其存放元数据(key,current num of conns per key,zone size)的共享内存大小 ngx_http_limit_conn_module
limit_conn_log_level limit_conn_log_level error; http, server, location Sets the desired logging level for cases when the server limits the number of connections. This directive appeared in version 0.8.18. 指定当触发limit的时候日志打印级别 ngx_http_limit_conn_module

实例

http {
  limit_conn_zone $binary_remote_addr zone=ips:10m;
  limit_conn_zone $server_name zone=servers:10m;
  limit_conn_log_level notice;
  server {
    # these limits apply to the whole virtual server
    limit_conn ips 10;

    # only 1000 simultaneous connections to the same server_name
    limit_conn servers 1000;
  }
}

limit_req

名称 默认配置 作用域 官方说明 中文解读 模块
limit_req 没有默认值,语法 limit_req zone=name [burst=number] [nodelay]; http, server, location Sets the shared memory zone and the maximum burst size of requests. If the requests rate exceeds the rate configured for a zone, their processing is delayed such that requests are processed at a defined rate. Excessive requests are delayed until their number exceeds the maximum burst size in which case the request is terminated with an error. 指定zone的burst大小 ngx_http_limit_req_module
limit_req_zone 没有默认值,语法 limit_req_zone key zone=name:size rate=rate; http Sets parameters for a shared memory zone that will keep states for various keys. In particular, the state stores the current number of excessive requests. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted. 第一个参数指定key,第二个参数指定zone名称和元数据的内存大小,第三个参数rate指定单位时间的请求数阈值 ngx_http_limit_req_module
limit_req_log_level limit_req_log_level error; http, server, location Sets the desired logging level for cases when the server refuses to process requests due to rate exceeding, or delays request processing. Logging level for delays is one point less than for refusals. 指定触发req limit时打印的日志级别 ngx_http_limit_req_module

实例

http {
 limit_req_zone $binary_remote_addr zone=myreqzone:10m
 limit_req_log_level warn;
 server {
  ## 每个ip限定10个连接数
  ## 正常一个浏览器给每个host开两到三个连接
  ## 触发的话会返回503
  ## nodelay表示一上来就直接计算,不经过一些预热后再计算
  limit_req zone=myreqzone burst=10 nodelay;
 }
}

以上就是我们整理的nginx中limit配置参数的全部内容,大家可以在下方的留言区讨论,感谢你对脚本之家的支持。

相关文章

  • Nginx 运维之域名验证的方法示例

    Nginx 运维之域名验证的方法示例

    这篇文章主要介绍了Nginx 运维之域名验证的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Nginx进程调度问题详解

    Nginx进程调度问题详解

    Nginx采用的是固定数量的多进程模型,由一个主进程(MasterProcess)和数量与主机CPU核数相同的工作进程协同处理各种事件。这篇文章主要介绍了Nginx进程调度问题,需要的朋友可以参考下
    2021-09-09
  • Nginx根据不同浏览器语言配置页面跳转的方法

    Nginx根据不同浏览器语言配置页面跳转的方法

    这篇文章主要介绍了Nginx根据不同浏览器语言配置页面跳转的方法,包括一个简体繁体的基本判断方法及实际根据中英文跳转的例子,需要的朋友可以参考下
    2016-04-04
  • nginx代理转发报错405 Method Not Allowed解决

    nginx代理转发报错405 Method Not Allowed解决

    这篇文章主要为大家介绍了解决nginx代理转发报错405 Method Not Allowed解决方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • 一句简单命令重启nginx

    一句简单命令重启nginx

    最近我的多个VPS经常出现502错误,经常需要重启nginx,但网上的很多教程都需要繁琐的启动脚本,远不如apache的重启命令那么简单。
    2010-03-03
  • Nginx配置文件解析

    Nginx配置文件解析

    Nginx 是一款常用的 Web 服务器软件,其配置文件用于指定服务器的行为和功能,本文将给大家详细的解析Nginx配置文件,文中有相关的代码示例供大家参考,需要的朋友可以参考下
    2023-09-09
  • Nginx服务器中配置非80端口的端口转发方法详解

    Nginx服务器中配置非80端口的端口转发方法详解

    这篇文章主要介绍了Nginx服务器中配置非80端口的端口转发方法详解,文中使用到了Nginx中的proxy_pass配置项,需要的朋友可以参考下
    2016-04-04
  • Nginx解决403 forbidden的完整步骤

    Nginx解决403 forbidden的完整步骤

    这篇文章主要给大家介绍了关于Nginx解决403 forbidden的完整步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Nginx如何配置Http、Https、WS、WSS的方法步骤

    Nginx如何配置Http、Https、WS、WSS的方法步骤

    这篇文章主要介绍了Nginx如何配置Http、Https、WS、WSS的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • 探究Nginx中reload流程的原理真相

    探究Nginx中reload流程的原理真相

    这篇文章主要介绍了探究Nginx中reload流程的原理真相,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论