Nginx限流配置的幾種方案的使用小結(jié)
Nginx為我們提供了請求限制模塊(ngx_http_limit_req_module)、基于令牌桶算法的流量限制模塊(ngx_stream_limit_conn_module),可以方便的控制令牌速率,自定義調(diào)節(jié)限流,實現(xiàn)基本的限流控制
此模塊已經(jīng)合并至主線版本中,無需再額外編譯添加
一、限制每個用戶流量(并發(fā)限制)
Nginx 并發(fā)限制的功能來自于 ngx_http_limit_conn_module 模塊
模塊的文檔:Module ngx_http_limit_conn_module
limit_conn_zone 只能配置在 http 范圍內(nèi),可同時配置多條,被不同所引用;
$binary_remote_addr 表示客戶端請求的IP地址;
one 自己定義的變量名(緩沖區(qū));
size 設(shè)置為1m,大約為16000個ip地址(詳細(xì)見文檔)
limit_rate 限制傳輸速度
limit_conn 與 limit_conn_zone 對應(yīng),限制網(wǎng)絡(luò)連接數(shù)
1、在http體添加配置說明
http
{
limit_conn_zone $binary_remote_addr zone=one:1m; # 限速定義
}2、在server體添加限速實現(xiàn)
server{
limit_conn one 1; #限制每個ip只能發(fā)起一個并發(fā)連接
limit_rate 256k; #限制每個連接的限制速度為256K,IP的下載速度為連接數(shù)*限制速度
}說明:為了減輕后端壓力正常限制在接口層就可以
二、限制每個IP限定時間的訪問次數(shù)(請求限制)
請求限制的功能來自于 ngx_http_limit_req_module 模塊
模塊文檔:Module ngx_http_limit_req_module
limit_req_zone 只能配置在 http 范圍內(nèi);
$binary_remote_addr 表示客戶端請求的IP地址;
mylimit 自己定義的變量名;
size 設(shè)置為1m,大約為16000個ip地址(詳細(xì)見文檔)
rate 請求頻率,每秒允許多少請求;
limit_req 與 limit_req_zone 對應(yīng)
burst 是配置超額處理,可簡單理解為隊列機(jī)制,讓多余的請求可以先放到隊列里,如果不加nodelay參數(shù),隊列里的請求不會立即處理,而是按照rate設(shè)置的速度,以毫秒級精確的速度慢慢處理
nodelay 參數(shù)允許請求在排隊的時候就立即被處理,也就是說只要請求能夠進(jìn)入burst隊列,就會立即被后臺worker處理,請注意,這意味著burst設(shè)置了nodelay時,系統(tǒng)瞬間的請求可能會超過rate設(shè)置的閾值。nodelay參數(shù)要跟burst一起使用才有作用
1、在http體添加配置說明
http
{
limit_req_zone $binary_remote_addr zone=mylimit:1m rate=5r/s; #限人數(shù)定義
}2、在server體添加限速實現(xiàn)
server{
limit_req zone=mylimit burst=100 nodelay; #限人數(shù)實現(xiàn)
}三、說明
1、整體限制就把限速實現(xiàn)放在sever層入口
2、限制接口訪問次數(shù)就放在接口配置
參考:
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; #限人數(shù)定義
}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只能發(fā)起一個并發(fā)連接
#limit_rate 256k; #限制每個連接的限制速度為256K,IP的下載速度為連接數(shù)*限制速度
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)$) ## ????ò3??2??o′?htmloíhtm?á?2μ????t
{
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/;
#限速實現(xiàn) 控制在接口層
# limit_conn one 1; #限制每個ip只能發(fā)起一個并發(fā)連接
# limit_rate 10k; #限制每個連接的限制速度為256K,IP的下載速度為連接數(shù)*限制速度
#限人數(shù)實現(xiàn)
# limit_req zone=mylimit burst=100 nodelay;
}
}
}到此這篇關(guān)于Nginx限流配置的幾種方案的使用小結(jié)的文章就介紹到這了,更多相關(guān)Nginx限流配置方案內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Nginx反向代理WebSocket響應(yīng)403的解決辦法
本篇文章主要介紹了詳解Nginx反向代理WebSocket響應(yīng)403的解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Apache Nginx 禁止目錄執(zhí)行PHP腳本文件的方法
這篇文章主要介紹了Apache Nginx 禁止目錄執(zhí)行PHP腳本文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
使用nginx如何解決Access-Control-Allow-Origin問題
這篇文章主要介紹了使用nginx如何解決Access-Control-Allow-Origin問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

