我使用 Nginx 作为反向代理。
如何限制 Nginx 中的上传速度?
问问题
2902 次
1 回答
2
分享一下如何在Nginx中限制反向代理的上传速度。
限制下载速度是小菜一碟,但对于上传却不是。
这是限制上传速度的配置
- 找到你的目录
/etc/nginx/nginx.conf
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;
}
}
}
如果你的 Nginx 不支持stream
.
您可能需要添加一个模块。
静止的:
$ ./configure --with-stream
$ make && sudo make install
动态的
$ ./configure --with-stream=dynamic
- https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/
注意:
如果你有HAProxy等客户端,Nginx作为服务器。
您可能会504 timeout
在上传大文件时遇到 HAProxy 和499 client is close
Nginx 中限制低上传速度的问题。
您应该timeout server: 605s
在 HAProxy 中增加或添加或超过,因为我们希望 HAProxy 在 Nginx 忙于上传到您的服务器时不要关闭连接。
https://stackoverflow.com/a/44028228/10258377
一些参考资料:
- https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_upload_rate
- http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_limit_rate
你会通过添加第三方模块来限制上传速度的一些其他方法,但是它很复杂并且不能正常工作
- https://www.nginx.com/resources/wiki/modules/
- https://www.nginx.com/resources/wiki/modules/upload/
- https://github.com/cfsego/limit_upload_rate
晚点再谢我 ;)
于 2020-12-24T04:29:46.043 回答