zoukankan      html  css  js  c++  java
  • How to limit the upload speed in Nginx

    nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发或负载均衡。This module is not built by default, it should be enabled with the --with-stream configuration parameter.

    关键参数

    Syntax: proxy_upload_rate rate;
    Default:
    proxy_upload_rate 0;
    Context: streamserver

    This directive appeared in version 1.9.3.

    Limits the speed of reading the data from the client. The rate is specified in bytes per second. The zero value disables rate limiting. The limit is set per a connection, so if the client simultaneously opens two connections, the overall rate will be twice as much as the specified limit.

    Parameter value can contain variables (1.17.0). It may be useful in cases where rate should be limited depending on a certain condition:

    map $slow $rate {
        1     4k;
        2     8k;
    }
    
    proxy_upload_rate $rate;




    
    


    示例配置

     1 events {
     2     worker_connections 1;
     3 }
     4 
     5 # 1)
     6 # Add a stream
     7 # This stream is used to limit upload speed
     8 stream {
     9 
    10     upstream site {
    11         server your.upload-api.domain1:8080;
    12         server your.upload-api.domain1:8080;
    13     }
    14 
    15     server {
    16 
    17         listen    12345;
    18 
    19         # 19 MiB/min = ~332k/s
    20         proxy_upload_rate 332k;
    21 
    22         proxy_pass site;
    23 
    24         # you can use directly without upstream
    25         # your.upload-api.domain1:8080;
    26     }
    27 }
    28 
    29 http {
    30 
    31   server {
    32     
    33     # 2)
    34     # Proxy to the stream that limits upload speed
    35     location = /upload {
    36         
    37         # It will proxy the data immediately if off
    38         proxy_request_buffering off;
    39         
    40         # It will pass to the stream
    41         # Then the stream passes to your.api.domain1:8080/upload?$args
    42         proxy_pass http://127.0.0.1:12345/upload?$args;
    43    
    44     }
    45 
    46     # You see? limit the download speed is easy, no stream
    47     location /download {
    48         keepalive_timeout  28800s;
    49         proxy_read_timeout 28800s;
    50         proxy_buffering off;
    51 
    52         # 75MiB/min = ~1300kilobytes/s
    53         proxy_limit_rate 1300k;
    54 
    55         proxy_pass your.api.domain1:8080;
    56     }
    57 
    58   }
    59 
    60 }
    -- Scott, Programmer in Beijing [If you can’t explain it to a six year old, you don’t understand it yourself. —Albert Einstein ]
  • 相关阅读:
    git 学习
    C语言 do while 语句
    C语言 计算班级平均成绩以及不及格的人数
    C语言 加减算法
    C语言 两个日期间的天数计算
    C语言 梯形面积
    C语言 while语句
    C语言 分段函数if else语句
    C语言 乘法运算
    C语言学习,for循环
  • 原文地址:https://www.cnblogs.com/scottgu/p/15105228.html
Copyright © 2011-2022 走看看