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 |
---|---|
Default: |
proxy_upload_rate 0; |
Context: |
stream , server
|
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 }