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 ]
  • 相关阅读:
    [置顶] java得到前一个月的年月日时分秒
    Windows 8.1 Preview的新功能和新API
    oracle保证读一致性原理
    10161
    Qt国际化
    使用Maven管理依赖JAR文件,自定义项目布局,利用ANT生成不同的发布包
    Haxe2.10到Haxe3,NME到OpenFL的迁移备忘
    设置RichEdit相关颜色说明
    使用MFC CImage类绘制PNG图片时遇到的问题
    把网球计分招式重构到状态模式
  • 原文地址:https://www.cnblogs.com/scottgu/p/15105228.html
Copyright © 2011-2022 走看看