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 ]
  • 相关阅读:
    暑假学习笔记(一)——初识Neo4j和APICloud入门
    置信规则库学习记录——1
    博客整理——宣传文案
    博客整理——软件工程实践总结
    博客整理——Alpha版冲刺
    博客整理——事后诸葛亮
    面试被企业拒绝后还有必要再申请吗?
    MapReduce分布式算法
    水题系列二:PhoneNumbers
    水题系列一:Circle
  • 原文地址:https://www.cnblogs.com/scottgu/p/15105228.html
Copyright © 2011-2022 走看看