heidsoft / cloud-bigdata-book

write book
56 stars 33 forks source link

nginx upload代理设置 #100

Open heidsoft opened 3 years ago

heidsoft commented 3 years ago
nginx stream 提供了四层代理。可以通过proxy_download_rate和proxy_upload_rate设置下载和上传限速。平时在七层http核心模块ngx_http_limit_conn_module这个模块提供limit_conn限速。
proxy_upload_rate 和proxy_download_rate分别配置从客户端读数据和从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速。
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;
    }

  }

}

How to reroute SFTP traffic via NGINX