nginx limit配置參數解讀 限速(帶寬畜埋、連接數缆瓣、請求數))

limit_rate

名稱默認配置作用域官方說明中文解讀模塊

limit_ratelimit_rate 0;http, server, location, if in locationLimits 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_afterlimit_rate_after 0;http, server, location, if in locationSets 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沒有默認值locationLimits 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_after1m;? ? ? ? ? ? limit_rate500k;? ? ? ? }? ? ? ? location / {? ? ? ? ? ? proxy_pass http://localhost:3000;limit_except GET {? ? ? ? ? ? ? ? deny all;? ? ? ? ? ? }? ? ? ? }

limit_conn

名稱默認配置作用域官方說明中文解讀模塊

limit_conn沒有默認值,語法 limit_conn zone number;http, server, locationSets 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;httpSets 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_levellimit_conn_log_level error;http, server, locationSets the desired logging level for cases when the server limits the number of connections. This directive appeared in version 0.8.18.指定當觸發(fā)limit的時候日志打印級別ngx_http_limit_conn_module

實例

http {? ? limit_conn_zone$binary_remote_addrzone=ips:10m;? ? limit_conn_zone$server_namezone=servers:10m;? ? limit_conn_log_level notice;? ? server {# these limits apply to the whole virtual serverlimit_conn ips 10;# only 1000 simultaneous connections to the same server_namelimit_conn servers 1000;? ? }}

limit_req

名稱默認配置作用域官方說明中文解讀模塊

limit_req沒有默認值,語法 limit_req zone=name [burst=number] [nodelay];http, server, locationSets 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;httpSets 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_levellimit_req_log_level error;http, server, locationSets 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.指定觸發(fā)req limit時打印的日志級別ngx_http_limit_req_module

實例

http { limit_req_zone$binary_remote_addrzone=myreqzone:10m limit_req_log_level warn; server {## 每個ip限定10個連接數## 正常一個瀏覽器給每個host開兩到三個連接## 觸發(fā)的話會返回503## nodelay表示一上來就直接計算,不經過一些預熱后再計算limit_req zone=myreqzone burst=10 nodelay; }}

doc

ngx_http_core_module

ngx_http_limit_conn_module

ngx_http_limit_req_module



limit_conn (限制客戶端的并發(fā)連接數)測試方法:

配置如下:

server {

? ? ? ? listen 5657;

? ? ? ? server_name server_bb;

? ? ? ? limit_rate 100k;

? ? ? ? limit_conn server_bb 1;

? ? ? ? location / {

? ? ? ? ? ? root html;

? ? ? ? }?

? ? }

}

curl一個大文件:

curl http://192.168.137.101:5657/test

之后查看nginx上的連接數

[root@fanpf-resty conf]# netstat -anpt | grep 192.168.137.201 | grep? ESTABLISHED | wc -l

1

再次curl一個大文件:

curl http://192.168.137.101:5657/test

返回503:

<html>

<head><title>503 Service Temporarily Unavailable</title></head>

<body>

<center><h1>503 Service Temporarily Unavailable</h1></center>

<hr><center>openresty/1.15.8.1rc1</center>

</body>

</html>

證明每個客戶端限制1個鏈接成功了材泄。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末沮焕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子拉宗,更是在濱河造成了極大的恐慌峦树,老刑警劉巖辣辫,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異魁巩,居然都是意外死亡急灭,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門歪赢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來化戳,“玉大人,你說我怎么就攤上這事埋凯〉懵ィ” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵白对,是天一觀的道長掠廓。 經常有香客問我,道長甩恼,這世上最難降的妖魔是什么蟀瞧? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮条摸,結果婚禮上悦污,老公的妹妹穿的比我還像新娘。我一直安慰自己钉蒲,他們只是感情好切端,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著顷啼,像睡著了一般踏枣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上钙蒙,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天茵瀑,我揣著相機與錄音,去河邊找鬼躬厌。 笑死马昨,一個胖子當著我的面吹牛,可吹牛的內容都是我干的扛施。 我是一名探鬼主播偏陪,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼煮嫌!你這毒婦竟也來了笛谦?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎逞刷,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體颂砸,經...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡灶轰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年谣沸,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片笋颤。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡乳附,死狀恐怖,靈堂內的尸體忽然破棺而出伴澄,到底是詐尸還是另有隱情赋除,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布非凌,位于F島的核電站举农,受9級特大地震影響,放射性物質發(fā)生泄漏敞嗡。R本人自食惡果不足惜颁糟,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喉悴。 院中可真熱鬧棱貌,春花似錦、人聲如沸箕肃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽突雪。三九已至,卻和暖如春涡贱,著一層夾襖步出監(jiān)牢的瞬間咏删,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工问词, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留督函,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓激挪,卻偏偏與公主長得像辰狡,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子垄分,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354