最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx限流配置的幾種方案的使用小結(jié)

 更新時(shí)間:2025年05月21日 10:30:28   作者:DK_Allen  
Nginx為我們提供了請(qǐng)求限制模塊、基于令牌桶算法的流量限制模塊,可以方便的控制令牌速率,自定義調(diào)節(jié)限流,實(shí)現(xiàn)基本的限流控制,下面就來介紹一下

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)文章

  • 淺析Nginx 負(fù)載均衡4種模式

    淺析Nginx 負(fù)載均衡4種模式

    這篇文章主要介紹了Nginx 負(fù)載均衡4種模式,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-06-06
  • 詳解Nginx反向代理WebSocket響應(yīng)403的解決辦法

    詳解Nginx反向代理WebSocket響應(yīng)403的解決辦法

    本篇文章主要介紹了詳解Nginx反向代理WebSocket響應(yīng)403的解決辦法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Nginx配置proxy_pass后返回404的問題及解決

    Nginx配置proxy_pass后返回404的問題及解決

    這篇文章主要介紹了Nginx配置proxy_pass后返回404的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Apache Nginx 禁止目錄執(zhí)行PHP腳本文件的方法

    Apache Nginx 禁止目錄執(zhí)行PHP腳本文件的方法

    這篇文章主要介紹了Apache Nginx 禁止目錄執(zhí)行PHP腳本文件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Nginx靜態(tài)資源或者路徑鑒權(quán)方式

    Nginx靜態(tài)資源或者路徑鑒權(quán)方式

    這篇文章主要介紹了Nginx靜態(tài)資源或者路徑鑒權(quán)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • nginx配置文件nginx.conf中文注釋說明

    nginx配置文件nginx.conf中文注釋說明

    nginx配置文件nginx.conf中文注釋說明,大家參考使用吧
    2013-12-12
  • 使用nginx如何解決Access-Control-Allow-Origin問題

    使用nginx如何解決Access-Control-Allow-Origin問題

    這篇文章主要介紹了使用nginx如何解決Access-Control-Allow-Origin問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • nginx如何根據(jù)用戶真實(shí)IP限源

    nginx如何根據(jù)用戶真實(shí)IP限源

    文章介紹了Nginx限制IP訪問的方式,包括通過remote_addr、X-Real-IP和X-Forwarded-For頭文件獲取用戶真實(shí)IP的方法,并詳細(xì)說明了如何配置Nginx以實(shí)現(xiàn)基于白名單的訪問控制
    2025-11-11
  • Nginx proxy_set_header配置方式

    Nginx proxy_set_header配置方式

    這篇文章主要介紹了Nginx proxy_set_header配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Nginx實(shí)現(xiàn)灰度發(fā)布的多種策略分享

    Nginx實(shí)現(xiàn)灰度發(fā)布的多種策略分享

    灰度發(fā)布是一種重要的策略,它允許我們?cè)诓挥绊懰杏脩舻那闆r下逐步推出新功能或更新,本文介紹了Nginx實(shí)現(xiàn)灰度發(fā)布的多種策略,有需要的小伙伴可以了解下
    2026-04-04

最新評(píng)論

云阳县| 宜城市| 天祝| 远安县| 桑植县| 宁南县| 深水埗区| 滨州市| 临武县| 苗栗市| 龙川县| 乌拉特前旗| 福海县| 汶上县| 北碚区| 新竹市| 东阳市| 阳朔县| 西吉县| 松桃| 双鸭山市| 鹤山市| 昭通市| 新源县| 祁连县| 阿克苏市| 四会市| 新兴县| 颍上县| 新昌县| 航空| 乌兰县| 慈利县| 新郑市| 壶关县| 青铜峡市| 时尚| 商南县| 灵川县| 原平市| 东源县|