Nginx實現限流
爲了防止用戶的惡意訪問,可以在在nginx設置限流,防止服務發生雪崩效應
Nginx限流分爲兩種
一是根據ip控制速率
二是控制併發連接數
1》 根據ip控制速率限流的配置
在http模塊添加配置
binary_remote_addr 是一種key,表示基於 remote_addr(客戶端IP) 來做限流,binary_ 的目的是壓縮內存佔用量。
zone:定義共享內存區來存儲訪問信息, contentRateLimit:10m 表示一個大小爲10M,名字爲 contentRateLimit的內存區域。
1M能存儲16000 IP地址的訪問信息,10M可以存儲16W IP地址訪問信息。
rate 用於設置最大訪問速率,rate=10r/s 表示每秒最多處理10個請求。
Nginx 實際上以毫秒爲粒度來跟蹤請求信息,因此 10r/s 實際上是限制:每100毫秒處理一個請求。這意味着,自上一個請求處理完後,若後續100毫秒內又有請求到達,將拒絕處理該請求.
給某個location配置limit_req
該配置的意思是 , 當請求路徑是/read_content時,會根據contentRateLimit來限流,
每個ip訪問的速率限制是2r/s,能夠突發訪問的請求數量是4,不延遲處理請求。
完整配置如下
複製代碼
user root root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#cache
lua_shared_dict dis_cache 128m;
#限流設置
limit_req_zone $binary_remote_addr zone=contentRateLimit:10m rate=2r/s;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location /update_content {
content_by_lua_file /root/lua/update_content.lua;
}
location /read_content {
limit_req zone=contentRateLimit burst=4 nodelay;
content_by_lua_file /root/lua/read_content.lua;
}
}}
2》根據併發連接數來限流
http模塊添加
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
location 添加配置
location / {
limit_conn perip 10;#單個客戶端ip與服務器的連接數.
limit_conn perserver 100; #限制與服務器的總連接數
root html; index index.html index.htm;
}
完整配置如下
user root root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#cache
lua_shared_dict dis_cache 128m;
#限流設置
limit_req_zone $binary_remote_addr zone=contentRateLimit:10m rate=2r/s;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location /update_content {
content_by_lua_file /root/lua/update_content.lua;
}
location /read_content {
limit_req zone=contentRateLimit burst=4 nodelay;
content_by_lua_file /root/lua/read_content.lua;
}
}
}
2》根據併發連接數來限流
http模塊添加
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
location 添加配置
location / {
limit_conn perip 10;#單個客戶端ip與服務器的連接數.
limit_conn perserver 100; #限制與服務器的總連接數
root html; index index.html index.htm;
}
完整配置如下
http {
include mime.types;
default_type application/octet-stream;
#cache
lua_shared_dict dis_cache 128m;
#限流設置
limit_req_zone $binary_remote_addr zone=contentRateLimit:10m rate=2r/s;
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#所有以brand開始的請求,單個客戶端ip與服務端的連接數是10,總共不超過100
location /brand {
limit_conn perip 10;#單個客戶端ip與服務器的連接數.
limit_conn perserver 100; #限制與服務器的總連接數
proxy_pass http://192.168.211.1:18081;
}
location /update_content {
content_by_lua_file /root/lua/update_content.lua;
}
location /read_content {
limit_req zone=contentRateLimit burst=4 nodelay;
content_by_lua_file /root/lua/read_content.lua;
}
}
}