Nginx限流配置的幾種方案的使用小結(jié)
Nginx為我們提供了請(qǐng)求限制模塊(ngx_http_limit_req_module)、基于令牌桶算法的流量限制模塊(ngx_stream_limit_conn_module),可以方便的控制令牌速率,自定義調(diào)節(jié)限流,實(shí)現(xiàn)基本的限流控制
此模塊已經(jīng)合并至主線版本中,無需再額外編譯添加
一、限制每個(gè)用戶流量(并發(fā)限制)
Nginx 并發(fā)限制的功能來自于 ngx_http_limit_conn_module 模塊
模塊的文檔:Module ngx_http_limit_conn_module
limit_conn_zone 只能配置在 http 范圍內(nèi),可同時(shí)配置多條,被不同所引用;
$binary_remote_addr 表示客戶端請(qǐng)求的IP地址;
one 自己定義的變量名(緩沖區(qū));
size 設(shè)置為1m,大約為16000個(gè)ip地址(詳細(xì)見文檔)
limit_rate 限制傳輸速度
limit_conn 與 limit_conn_zone 對(duì)應(yīng),限制網(wǎng)絡(luò)連接數(shù)
1、在http體添加配置說明
http { limit_conn_zone $binary_remote_addr zone=one:1m; # 限速定義 }
2、在server體添加限速實(shí)現(xiàn)
server{ limit_conn one 1; #限制每個(gè)ip只能發(fā)起一個(gè)并發(fā)連接 limit_rate 256k; #限制每個(gè)連接的限制速度為256K,IP的下載速度為連接數(shù)*限制速度 }
說明:為了減輕后端壓力正常限制在接口層就可以
二、限制每個(gè)IP限定時(shí)間的訪問次數(shù)(請(qǐng)求限制)
請(qǐng)求限制的功能來自于 ngx_http_limit_req_module 模塊
模塊文檔:Module ngx_http_limit_req_module
limit_req_zone 只能配置在 http 范圍內(nèi);
$binary_remote_addr 表示客戶端請(qǐng)求的IP地址;
mylimit 自己定義的變量名;
size 設(shè)置為1m,大約為16000個(gè)ip地址(詳細(xì)見文檔)
rate 請(qǐng)求頻率,每秒允許多少請(qǐng)求;
limit_req 與 limit_req_zone 對(duì)應(yīng)
burst 是配置超額處理,可簡(jiǎn)單理解為隊(duì)列機(jī)制,讓多余的請(qǐng)求可以先放到隊(duì)列里,如果不加nodelay參數(shù),隊(duì)列里的請(qǐng)求不會(huì)立即處理,而是按照rate設(shè)置的速度,以毫秒級(jí)精確的速度慢慢處理
nodelay 參數(shù)允許請(qǐng)求在排隊(duì)的時(shí)候就立即被處理,也就是說只要請(qǐng)求能夠進(jìn)入burst隊(duì)列,就會(huì)立即被后臺(tái)worker處理,請(qǐng)注意,這意味著burst設(shè)置了nodelay時(shí),系統(tǒng)瞬間的請(qǐng)求可能會(huì)超過rate設(shè)置的閾值。nodelay參數(shù)要跟burst一起使用才有作用
1、在http體添加配置說明
http { limit_req_zone $binary_remote_addr zone=mylimit:1m rate=5r/s; #限人數(shù)定義 }
2、在server體添加限速實(shí)現(xiàn)
server{ limit_req zone=mylimit burst=100 nodelay; #限人數(shù)實(shí)現(xiàn) }
三、說明
1、整體限制就把限速實(shí)現(xiàn)放在sever層入口
2、限制接口訪問次數(shù)就放在接口配置
參考:
http請(qǐng)求
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; #限制每個(gè)ip只能發(fā)起一個(gè)并發(fā)連接 #limit_rate 256k; #限制每個(gè)連接的限制速度為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(因?yàn)閕e低版本不支持) gzip_vary on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; # 測(cè)試中文 # 請(qǐng)求體最大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/; #限速實(shí)現(xiàn) 控制在接口層 # limit_conn one 1; #限制每個(gè)ip只能發(fā)起一個(gè)并發(fā)連接 # limit_rate 10k; #限制每個(gè)連接的限制速度為256K,IP的下載速度為連接數(shù)*限制速度 #限人數(shù)實(shí)現(xiàn) # limit_req zone=mylimit burst=100 nodelay; } } }
到此這篇關(guān)于Nginx限流配置的幾種方案的使用小結(jié)的文章就介紹到這了,更多相關(guān)Nginx限流配置方案內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
強(qiáng)大的 Web 應(yīng)?服務(wù)器OpenResty安裝(Nginx倉庫)
OpenResty 是?個(gè)強(qiáng)大的 Web 應(yīng)?服務(wù)器,Web 開發(fā)?員可以使用 Lua 腳本語?調(diào)動(dòng) Nginx ?持的各種 C 以及 Lua 模塊,更主要的是在性能方面,OpenResty可以快速構(gòu)造出足以勝任 10K 以上并發(fā)連接響應(yīng)的超高性能 Web 應(yīng)用系統(tǒng)2023-06-06詳解實(shí)現(xiàn)Nginx+Tomcat實(shí)現(xiàn)單IP、多域名、多站點(diǎn)的訪問
這篇文章主要介紹了詳解實(shí)現(xiàn)Nginx+Tomcat實(shí)現(xiàn)單IP、多域名、多站點(diǎn)的訪問的相關(guān)資料,這里提供實(shí)例幫助到大家實(shí)現(xiàn)改功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08