Nginx | 限制上传速度

使用nginx限制上传速度的配置。

资料来自Stack Overflow

没啥好写的,搬运一下。

首先要确保Nginx是支持stream的,不然只能重装。

1
2
3
4
5
6
7
8
static:

$ ./configure --with-stream
$ make && sudo make install
dynamic

$ ./configure --with-stream=dynamic
https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/

然后就是改Nginx配置做限速。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
worker_connections 1;
}

# 1)
# Add a stream
# This stream is used to limit upload speed
stream {

upstream site {
server your.upload-api.domain1:8080;
server your.upload-api.domain1:8080;
}

server {

listen 12345;

# 19 MiB/min = ~332k/s
proxy_upload_rate 332k;

proxy_pass site;

# you can use directly without upstream
# your.upload-api.domain1:8080;
}
}

http {

server {

# 2)
# Proxy to the stream that limits upload speed
location = /upload {

# It will proxy the data immediately if off
proxy_request_buffering off;

# It will pass to the stream
# Then the stream passes to your.api.domain1:8080/upload?$args
proxy_pass http://127.0.0.1:12345/upload?$args;

}

# You see? limit the download speed is easy, no stream
location /download {
keepalive_timeout 28800s;
proxy_read_timeout 28800s;
proxy_buffering off;

# 75MiB/min = ~1300kilobytes/s
proxy_limit_rate 1300k;

proxy_pass your.api.domain1:8080;
}

}

}